event.waitUntil 在 service worker 中做什么,为什么需要它?
What does event.waitUntil do in service worker and why is it needed?
MDN 建议您执行以下操作来创建和填充服务工作者缓存:
this.addEventListener('install', function(event) {
event.waitUntil(
caches.open('v1').then(function(cache) {
return cache.addAll([
'/sw-test/',
'/sw-test/index.html',
... etc ...
]);
})
);
});
我不明白那个代码。 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.
我不明白的是:
waitUntil
通常如何影响代码流?它会阻止事件传播直到它的承诺解决吗?
- 为什么在打开工作缓存的上下文中需要它?
我问这个问题是因为我上面的代码有问题,我想了解它。
正如描述所说:
the ExtendableEvent.waitUntil()
method extends the lifetime of the event.
如果您不在方法中调用它,服务工作者可能会随时停止(参见 the specification)。
因此,waitUntil
方法用于告诉浏览器在传递给 waitUntil
的承诺被解决或拒绝之前不要终止 service worker。
关于您的具体问题:
- 在
install
和 activate
事件的情况下,它延迟 service worker 的状态切换到 installed
和 activated
(参见 specification of the waitUntil method,特别是该段的最后一部分)。
- 我想我的其余回答已经回答了为什么需要它。
MDN 建议您执行以下操作来创建和填充服务工作者缓存:
this.addEventListener('install', function(event) {
event.waitUntil(
caches.open('v1').then(function(cache) {
return cache.addAll([
'/sw-test/',
'/sw-test/index.html',
... etc ...
]);
})
);
});
我不明白那个代码。 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.
我不明白的是:
waitUntil
通常如何影响代码流?它会阻止事件传播直到它的承诺解决吗?- 为什么在打开工作缓存的上下文中需要它?
我问这个问题是因为我上面的代码有问题,我想了解它。
正如描述所说:
the
ExtendableEvent.waitUntil()
method extends the lifetime of the event.
如果您不在方法中调用它,服务工作者可能会随时停止(参见 the specification)。
因此,waitUntil
方法用于告诉浏览器在传递给 waitUntil
的承诺被解决或拒绝之前不要终止 service worker。
关于您的具体问题:
- 在
install
和activate
事件的情况下,它延迟 service worker 的状态切换到installed
和activated
(参见 specification of the waitUntil method,特别是该段的最后一部分)。 - 我想我的其余回答已经回答了为什么需要它。