You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are cases when we would like to schedule some work after a response has been sent. For my use case, I am writing a Discord bot that performs potentially long-running work. It is thus necessary for me to acknowledge an incoming slash command immediately (i.e., send an HTTP response) and defer a more complete response later (i.e., eventually via the Discord API).
Currently, the Discord API provides an endpoint that lets us edit the original response for a slash command response. Unfortunately, as there is no way to dispatch the work after a response has been sent, there is a fairly common race condition where editing the original response may come first before the original response is even returned from the route handler. Since the slash command response is yet to be received by Discord, invoking the API endpoint for editing the original response (rightly) results in a 404.
Describe the proposed solution
SvelteKit needs an API for scheduling work after a response has been sent. In the context of my use case, this makes sure that editing the original response comes after the original one has been sent. I believe a solution like the after API in Next.js is ideal here.
Alternatives considered
Currently, the way I hacked through this problem is to introduce an artificial timeout. This is far from ideal, of course...
import{json}from'@sveltejs/kit';import{setTimeout}from'node:timers/promises';// +server.jsasyncfunctionPOST(...){// Handle the slash command and preserve some context.const{ result, ctx }=awaithandleSlashCommand(...);// Detached promise runs in the background.void(async()=>{// HACK: Wait for the response to be sent.awaitsetTimeout(1000);// Use the existing context to edit the response eventually.awaitdoPostResponseWork(ctx, ...);})();// Send the initial response immediately.returnjson(result);}
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered:
I think this would have to be something the adapter would have to implement... waiting until after a request is served isn't possible on all platforms.
The Node adapter probably just works if you choose not to await some work while the server still runs after the response is sent.
Yup! This is my current workaround, too. Though, I would prefer a cleaner API that hooks into the request-response lifecycle such that the async work only gets triggered when the response has finished writing to the socket. The fire-and-forget promise doesn't have that guarantee.
Describe the problem
There are cases when we would like to schedule some work after a response has been sent. For my use case, I am writing a Discord bot that performs potentially long-running work. It is thus necessary for me to acknowledge an incoming slash command immediately (i.e., send an HTTP response) and defer a more complete response later (i.e., eventually via the Discord API).
Currently, the Discord API provides an endpoint that lets us edit the original response for a slash command response. Unfortunately, as there is no way to dispatch the work after a response has been sent, there is a fairly common race condition where editing the original response may come first before the original response is even returned from the route handler. Since the slash command response is yet to be received by Discord, invoking the API endpoint for editing the original response (rightly) results in a 404.
Describe the proposed solution
SvelteKit needs an API for scheduling work after a response has been sent. In the context of my use case, this makes sure that editing the original response comes after the original one has been sent. I believe a solution like the
after
API in Next.js is ideal here.Alternatives considered
Currently, the way I hacked through this problem is to introduce an artificial timeout. This is far from ideal, of course...
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: