Delay ctx.waitUntil() until fetch() has completed or canceled
Hi! I'm wondering if anyone has a method for delaying the execution of the promise passed into
ctx.waitUntil()
until the primary fetch()
has completed or canceled?6 Replies
Create an empty Promise, await it in waitUntil, then only resolve it once you start to return Response
Thanks for the reply @HardlyHolidayin’ ! If the client disconnects before the Promise is resolved will the
ctx.waitUntil()
still be executed?Not sure? The entire isolate may be aborted. This might be a try it and see thing
Gotcha, I will check it out. I’m trying to run some code regardless of whether the client disconnected, which is hard because the runtime doesn’t send a signal about disconnects
But I know that
ctx.waitUntil()
can extend the runtime in the event of a disconnect
update on this, I get a A hanging promise was canceled
error message when I try to await the empty promise in the ctx.waitUntil()
Hm…
I don't think you can deliberately delay until after fetch has completed, except that the worker is single-threaded, so if you launch some async io in the waituntil, return the response, if the response requires synchronous calculation, processing the waituntil will be delayed until the response yields