This topic provides answers to some commonly asked questions about the waitUntil method of EdgeRoutine (ER).
Why does a fetch request occasionally fail?
- Lifecycle of the context
When you call the
addEventListenermethod to register a callback function, the context is automatically created. After the Response object including the response header and body returned by the
event.respondWithmethod has been read, the context closes.
The lifecycle of all asynchronous functions is included in the lifecycle of contexts. An asynchronous function will await the execution of the Promise object until it is resolved. If you do not want asynchronous functions to wait the execution of the Promise object, call the event.waitUntil method. This method extends the lifecycle of a context until all the Promise objects passed to the waitUntil method are executed.
- Shared states among contexts
- Concurrent Promise objects
Promise objects that are not awaited by functions are concurrent. If you want ER to respond to browsers immediately after ER receives requests instead of awaiting subrequests, call the waitUntil method to ensure that programs run as expected.
- Exceptions thrown by asynchronous functions
If a subrequest in a waitUntil statement throws an exception, the context exits.