This topic provides answers to some commonly asked questions about the waitUntil method of EdgeRoutine (ER).
Why does a fetch request occasionally fail?
A fetch request may fail due to the following causes:
Lifecycle of the context
When you call the
addEventListener
method to register a callback function, the context is automatically created. After the Response object including the response header and body returned by theevent.respondWith
method has been read, the context closes.The lifecycle of all asynchronous functions is included in the lifecycle of contexts. An asynchronous function awaits the execution of the Promise object until the object 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 Promise objects passed to the waitUntil method are executed.
Shared states among contexts
States cannot be shared among contexts. Data that is not in standard JavaScript structures cannot be shared across contexts. For example, you cannot share data in the structure of the Service Worker API or Stream objects across contexts. If Context A uses an object that does not belong to Context A, ER detects the object and throws an exception.
NoteYou can use native JavaScript objects, such as String, Array, Object, and Number, or custom classes, to share states.
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.
Can I call the waitUntil method multiple times?
You can call the waitUntil method multiple times, or nest a waitUntil method inside another waitUntil method. This way, the Promise object returned by one of the waitUntil methods is resolved, and you can continue to call the waitUntil method in the callback function.
Contexts have a specific response time. If the response times out, the context exits.