Throw vs return in router’s action

Suppose I have an action that should redirect. In the docs(https://docs.solidjs.com/solid-router/reference/data-apis/action) I found example of both returning and throwing a redirect. Which should be used and when?
10 Replies
TaQuanMinhLong
TaQuanMinhLong2mo ago
just simply choose one that works :Worry_PatKEK: i personally go with throw redirect
bitobeats
bitobeatsOP2mo ago
So you’re saying they work the same way?
TaQuanMinhLong
TaQuanMinhLong2mo ago
:Worry_DontKnow: not sure but i prefer to throw as actions are meant to return results i don't think redirect is a result of something
bitobeats
bitobeatsOP2mo ago
Then why is it recommended in the docs?
TaQuanMinhLong
TaQuanMinhLong2mo ago
it should be some kind of value like {error} or {success: true} etc.. @Atila :Worry_DontKnow: i think i'll just let him handle this
bitobeats
bitobeatsOP2mo ago
No worries! Thanks for trying to help and bringing someone who may have this information to the discussion 😊
TaQuanMinhLong
TaQuanMinhLong2mo ago
:_Worry_Sir:
Atila
Atila2mo ago
thanks for the mention @TaQuanMinhLong the short answer is: you can return and throw the redirect and they'll most likely work well for you the longer answer is that throw in JS will stop the function execution, interrupting the program and propagating this effect updwards. While return will gracefully end and offer back the value. So, as you can imagine, when it comes to a redirect()it makes little difference, but you'd be right in expecting the throw to be a bit more predictable and immediate in that case. +1 on throw. I also think it's more idiomatic and it correlates well on how <Suspense> operates too Under the hood, Suspense throws a promise to allow it to propagate towards the outer scope - essentially that's what we want with a redirect, so things click better inside my head when I'm reasoning about the control flow. As you said, a redirect() isn't a directly related to the outcome of the action(), it's more of an "app state" thing. We send the user somewhere else and that interrupts the flow they were at, theoretically, it's "above the paygrade" of a simple form submission... so throwing makes more sense to me too
TaQuanMinhLong
TaQuanMinhLong2mo ago
:Worry_Stonk: then should we also fix the docs :Worry_Think:
Atila
Atila2mo ago
yeah, I think for the sake of consistency it could be better to have throw in the code snippets - perhaps add a note about how return/throw can work would be best. The Data Loading entries are overdue some work for sure
Want results from more Discord servers?
Add your server