Send an early response to an action, then do stuff in the background #3938
Replies: 3 comments 2 replies
-
You can do it before returning a response? Does it have to be done after? |
Beta Was this translation helpful? Give feedback.
-
Sorry but I have a authentic app on my phone And I have to go through that
…On Fri, Aug 5, 2022, 4:48 PM Dan Kirkham ***@***.***> wrote:
You can do it before returning a response? Does it have to be done after?
—
Reply to this email directly, view it on GitHub
<#3938 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ATHP7OHEVMM7GMQKIPTKQHTVXV42TANCNFSM55XCABVA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Your example of using finally would work, but just as in your express route, make sure you catch any errors. You could also do: somethingAsync().catch(console.error); // Be sure to catch to avoid unhandled rejections from exiting the server
return json({ ok: true }); Or if you don't even want the background processing to even begin until you've returned a response you can do: queueMicrotask(() => somethingAsync().catch(console.error));
return json({ ok: true }); However I’d only recommend this if it’s ok for your background processing to fail. If it’s critical for the action to be handled you’re better off pushing it to a persistent queue which has the ability to retry in the event of a failure / outage, e.g. https://github.com/taskforcesh/bullmq |
Beta Was this translation helpful? Give feedback.
-
Hello
With Express, I used to sometimes send a quick response to a request, then do something in the background, like when receiving a webhook for instance, or when I have some statistics to cache after a form completion.
I used to do something like that
however in the
action
, it seems that theResponse
is sent when wereturn
the function, is it ?So if I return, I can't do anything afterwards, is it ?
Or - I'm thinking out loud - could I use
finally
?would that be an appropriate pattern ?
Beta Was this translation helpful? Give feedback.
All reactions