什么会导致承诺被拒绝,并在'InvalidStateError'
这里?什么会导致Promise在这里被'InvalidStateError'拒绝?
const SERVICE_WORKER_VERSION = "3.0.0"; // is updated in the build when things change
const CACHE_VERSION = SERVICE_WORKER_VERSION;
const fillServiceWorkerCache = function() {
/* save in cache some static ressources
this happens before activation */
return caches.open(CACHE_VERSION).then(function(cache) {
return cache.addAll(ressourcesToSaveInCache);
});
};
self.addEventListener("install", function (event) {
/*event.waitUntil takes a promise that should resolves successfully*/
event.waitUntil(fillServiceWorkerCache().then(function() {
return self.skipWaiting();
}));
});
在Firefox版本52出现以下错误:Service worker event waitUntil() was passed a promise that rejected with 'InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable'.
服务工作者被杀害,之后取出。它适用于Chrome。 ressourcesToSaveInCache
是一组相对URL。
编辑将其更改为
event.waitUntil(
fillServiceWorkerCache()
.then(skipWaiting)
.catch(skipWaiting)
);
和服务人员登录!但fillServiceWorkerCache
被拒绝,这是一个大问题(没有脱机缓存)。现在的问题是为什么fillServiceWorkerCache
拒绝,以及试图告诉的错误信息是什么?
编辑由Hosar的回答启发:
const fillServiceWorkerCache2 = function() {
return caches.open(CACHE_VERSION).then(function (cache) {
return Promise.all(
ressourcesToSaveInCache.map(function (url) {
return cache.add(url).catch(function (reason) {
return console.log(url + "failed: " + String(reason));
})
})
);
});
};
这个版本传播在返回链中的承诺,使waitUntil()
真正等待它。它不会缓存,也不会拒绝缓存中未能添加的单个资源。
编辑2:在ressourcesToSaveInCache固定无效相对URL后,错误不见了