Executing code after sending an HTTP response in Express can be done like this:
res.send()
await someAsyncFunction() // assuming this function takes a significant amount of time
In the case of Next.js, testing locally shows that the above code behaves similarly to Express. However, when deployed on Vercel, it appears that the code stops executing after the HTTP response is sent. It's unclear whether this is due to the serverless functions setup or something else. As a workaround, the code needs to be rearranged as follows:
await someAsyncFunction() // assuming this function takes a significant amount of time
res.send()
The challenge with this approach is that if the async function is slow, there is a risk of the response timing out before it is sent back. This becomes problematic in scenarios where immediate response delivery is crucial. For instance, when utilizing a rate-limited API to send multiple emails, which could potentially take a long time, it becomes necessary to send an HTTP response promptly before proceeding with the time-consuming email-sending process.