Next.js Discord

Discord Forum

Why doesn't redirect work in async functions?

Unanswered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I tried running a normal function with redirect('/') and it works, but when it is async, it throws a NEXT_REDIRECT error. Why can't redirect be in async functions?

6 Replies

are you using try catch? (as you may either need to re-throw it)
btw it purposefully throws error
Invoking the redirect() function throws a NEXT_REDIRECT error and terminates rendering of the route segment in which it was thrown. [[docs](https://nextjs.org/docs/app/api-reference/functions/redirect)]
@American black bear I tried running a normal function with redirect('/') and it works, but when it is async, it throws a NEXT_REDIRECT error. Why can't redirect be in async functions?
It can't be run in an async function, only in the part where React Errors can be caught in error boundaries
ohhh i was thinking async function was a async component (ie server)
its what joulev said a couple of weeks ago
i forgot how to do it in the client component async event callback