The ExtendableEvent.waitUntil()
method extends the lifetime of the event. In service workers, extending the life of an event prevents the browser from terminating the service worker before asynchronous operations within the event have completed.
When called in an EventHandler
associated to the install
event, it delays treating the installed worker as installing
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. N
ote 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
event.waitUntil(promise)
Returns
None.
Parameters
A Promise
.
Example
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 |
async waitUntil() |
? | 53.0 (53.0)[3] | No support | ? | No support |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | No support | No support | 44.0 (44.0) | No support | ? | No support | 40.0 [1] |
async waitUntil() |
No support | 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
- Using Service Workers
- Service workers basic code example
- Is ServiceWorker ready?
Promise
- Using web workers