Can you call context.waitUntil() multiple times?

After handling a request, I have to save something to KV and make a fetch call to some other service. The order in which they execute does not matter. My code looks like this
async function handler(request, env, context) {
/// generate response...

context.waitUntil(saveResultToKV(stuff, env))
context.waitUntil(callExternalService(env))

return response;
}
async function handler(request, env, context) {
/// generate response...

context.waitUntil(saveResultToKV(stuff, env))
context.waitUntil(callExternalService(env))

return response;
}
Will the runtime wait for all the promises passed to context.waitUntil? Or will the runtime stop when the first promise resolves?
2 Replies
Chaika
Chaika8mo ago
Yes, you could also do waitUntil(Promise.all(...))
Arma
Arma8mo ago
Whats the runtime behaviour when I'm calling waitUntil() twice? It does something like Promise.allSettled(...) internally ? Thanks! The docs should mention that it will wait for all of them to be settled, indeed
Want results from more Discord servers?
Add your server