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 1180274).

Syntax

extendableEvent.waitUntil(promise);

Parameters

A Promise.

Return value

undefined.

Example

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

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

Specifications

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

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
waitUntil
Experimental
Chrome Full support 40Edge Full support 17Firefox Full support 44IE No support NoOpera Full support 24Safari No support NoWebView Android Full support 40Chrome Android Full support 40Firefox Android Full support 44Opera Android Full support 24Safari iOS No support NoSamsung Internet Android Full support 4.0
Asynchronous waitUntil
Experimental
Chrome ? Edge Full support 17Firefox Full support 53IE No support NoOpera ? Safari No support NoWebView Android ? Chrome Android ? Firefox Android Full support 53Opera Android ? Safari iOS No support NoSamsung Internet Android ?

Legend

Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

See also