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.

安装 events in service workers 使用 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.

activate events in service workers 使用 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.

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.

注意 : The behaviour described in the above paragraph was fixed in Firefox 43 (see bug 1180274 ).

句法

extendableEvent.waitUntil(promise);
					

参数

A Promise .

范例

使用 waitUntil() within a service worker's 安装 event:

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

规范

规范 状态 注释
服务工作者
The definition of 'waitUntil()' in that specification.
工作草案 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
waitUntil Chrome 40 Edge 17 Firefox 44 IE No Opera 24 Safari No WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 24 Safari iOS No Samsung Internet Android 4.0
异步 waitUntil Chrome ? Edge 17 Firefox 53 IE No Opera ? Safari No WebView Android ? Chrome Android ? Firefox Android 53 Opera Android ? Safari iOS No Samsung Internet Android ?

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: