What does event.waitUntil do in service worker and why is it needed?

孤人 提交于 2019-12-03 00:58:16

As the description says, the ExtendableEvent.waitUntil() method extends the lifetime of the event. If you don't call it inside a method, the service worker could be stopped at any time (see the specification).

So, the waitUntil method is used to tell the browser not to terminate the service worker until the promise passed to waitUntil is either resolved or rejected.

About your specific questions:

  • In the case of the install and the activate events, it delays the state switch of the service worker to installed and activated (see the specification of the waitUntil method, in particular the last part of the paragraph).
  • I think the rest of my answer already answered as to why it is needed.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!