W3cubDocs

/DOM

ExtendableEvent.waitUntil

The extendableEvent.waitUntil() method tells the event dispatcher that work is ongoing. It can also be used to detect whether that work was successful. In service workers, waitUntil() tells the browser that work is ongoing until the promise settles, and it shouldn't terminate the service worker if it wants that work to complete.

The install events in service workers use waitUntil() to hold the service worker in the installing phase until tasks complete. If the promise passed to waitUntil() rejects, the install is considered a failure, and the installing service worker is discarded. This is primarily used to ensure that a service worker is not considered installed until all of the core caches it depends on are successfully populated.

The activate events in service workers use waitUntil() to buffer functional events such as fetch and push until the promise passed to waitUntil() settles. This gives the service worker time to update database schemas and delete outdated caches, so other events can rely on a completely upgraded state.

The waitUntil() method must be initially called within the event callback, but after that it can be called multiple times, until all the promises passed to it settle.

Note: The behaviour described in the above paragraph was fixed in Firefox 43 (see bug 1189644).

Syntax

event.waitUntil(promise)

Parameters

A Promise.

Return value

None.

Example

Using waitUntil() within a service worker's install event:

addEventListener('install', event => {
  event.waitUntil(async function() {
    const cache = await caches.open('static-v1');
    await cache.addAll([
      '/', '/about/', '/static/styles.css'
    ]);
  }());
});

Specifications

Specification Status Comment
Service Workers
The definition of 'waitUntil()' in that specification.
Editor's Draft Initial definition

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 40[1] 44.0 (44.0)[2] No support 24 No support
async waitUntil() ? 53.0 (53.0)[3] No support ? No support
Feature Android Webview Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 40[1] 40[1] 44.0 (44.0) No support ? No support
async waitUntil() No support ? 53.0 (53.0)[3] No support ? No support

[1] Before Chrome 46, waitUntil() would take any value rather than just promises.

[2] Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)

[3] ExtendableEvent.waitUntil() can now be called asynchronously (see bug 1263304).

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ExtendableEvent/waitUntil