Make sure non-blocking function can actually complete

I have a SvelteKit API route in this pattern:
export const GET = async () => {
const data = await doSomething();

somethingElse();

return new Response(data);
};
export const GET = async () => {
const data = await doSomething();

somethingElse();

return new Response(data);
};
How do I make sure that somethingElse (which makes an async HTTP request) actually completes? This pattern works in local dev, but in the runtime it seems to work only intermittently. How can I keep somethingElse non-blocking while still making sure it actually finishes?
2 Replies
Cyb3r-Jak3
Cyb3r-Jak37mo ago
You need to use waitUntil I think the code would roughly be something like
export const GET = async ({context}) => {
const data = await doSomething();

context.waitUntil(somethingElse());

return new Response(data);
};
export const GET = async ({context}) => {
const data = await doSomething();

context.waitUntil(somethingElse());

return new Response(data);
};
llamaz
llamazOP7mo ago
solved, I had to use destructured platform and .context.waitUntil

Did you find this page helpful?