Эта статья нуждается в техническом обзоре. Как вы можете помочь.
Наши волонтёры ещё не перевели данную статью на Русский. Присоединяйтесь к нам и помогите закончить эту работу!
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 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
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 |
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] |
- [1] Before Chrome 46,
waitUntil()
would take any value rather than justpromises
. - [2] Service workers (and Push) have been disabled in the Firefox 45 Extended Support Release (ESR.)
See also
- Using Service Workers
- Service workers basic code example
- Is ServiceWorker ready?
Promise
- Using web workers