Answers to commonly asked questions about the waitUntil method in EdgeRoutine (ER).
Why does a fetch request occasionally fail?
A fetch request may fail due to the following reasons:
-
Lifecycle of the context
When you call
addEventListenerto register an event callback function, a context is automatically created. The context ends after the Response object returned byevent.respondWithis fully read, including the header and body.All asynchronous functions share the lifecycle of their context. An asynchronous function awaits the execution of the Promise object until it resolves. To prevent asynchronous functions from blocking on a Promise, call the event.waitUntil method. This method extends the context lifecycle until all Promise objects passed to waitUntil are executed.
-
Shared states among contexts
Contexts are isolated and cannot share state. Non-standard JavaScript data structures, such as objects from the Service Worker API or
Streamobjects, cannot be shared across contexts. If one context attempts to use an object that belongs to another, EdgeRoutine detects this and throws an exception.NoteYou can use native JavaScript objects, such as String, Array, Object, and Number, or custom classes to share state across contexts.
-
Concurrent Promise objects
Promise objects that are not awaited are concurrent. To have ER respond to browsers immediately without waiting for subrequests, call the waitUntil method to ensure that your program runs as expected.
-
Exceptions thrown by asynchronous functions
If a subrequest in a waitUntil statement throws an exception, the context exits.
Can I call the waitUntil method multiple times?
You can call waitUntil multiple times. You can also nest waitUntil calls. For example, when a Promise passed to waitUntil resolves, you can call waitUntil again inside that Promise's callback function.
Each context has a time limit. If a context exceeds this limit, it is forcibly terminated.