ExtendableEvent.waitUntil()

The ExtendableEvent.waitUntil() method extends the lifetime of the event. When called in an EventHandler associated to the install event, it delays treating the installing worker as installed until the passed Promise resolves successfully. This is primarily used to ensure that a service worker is not considered installed until all of the core caches it depends on are populated.

When called in an EventHandler associated to the activate event, it delays treating the active worker as activated until the passed Promise resolves successfully. This is primarily used to ensure that any functional events are not dispatched to the ServiceWorkerGlobalScope object until it upgrades database schemas and deletes the outdated Cache entries.

When the method runs, if the Promise is resolved, nothing happens. If the Promise is rejected, the state of the installing or active worker is set to redundant.

If waitUntil() is called outside of the ExtendableEvent handler, the browser should throw an InvalidStateError; note also that multiple calls will stack up, and the resulting promises will be added to the list of extend lifetime promises.

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

Syntax

JavaScript
event.waitUntil(function)

Returns

None.

Parameters

A function that returns a Promise.

Example

js;highlight:[9,10,11,12,13,14,15]
self.addEventListener('push', function(event) {
  console.log('Received a push message', event);

  var title = 'Yay a message.';
  var body = 'We have received a push message.';
  var icon = '/images/icon-192x192.png';
  var tag = 'simple-push-demo-notification-tag';

  event.waitUntil(
    self.registration.showNotification(title, {
      body: body,
      icon: icon,
      tag: tag
    })
  );
});

Specifications

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

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 40.0[1] 44.0 (44.0)[2] No support 24 No support
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support No support 44.0 (44.0) (Yes) No support ? No support 40.0 [1]

See also

 

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/extendableevent/waituntil

API ExtendableEvent Method Reference waitUntil