I’m currently working on a service worker in typescript that needs to iterate through all the client caches, then iterate through each cache’s keys to determine if entries should be kept or deleted. After each entry in a cache has been evaluated, it should move on to the next cache until all finish. I can do it with a single cache, but when I try to map out all the caches it simply iterates through the cache keys and resolves the initial promise without completing each iteration. Clearly I have an issue with my promise chaining, but I’m not sure where. Here is a simplified version of the code.
cacheProcessing = new Promise<any>((resolveCacheProcessing, rejectCacheProcessing) => {
self.caches.keys().then((cacheList)=>{
const cacheCycle = Promise.all( //cache cycle promise
cacheList.map((cacheName) => {
console.log("Scanning cache: " + cacheName)
self.caches.open(cacheName).then((result)=>{
result.keys().then((keyList)=>{
return Promise.all( //key cycle promise
keyList.map((key)=>{
if(a){
return result.delete(key);
}
return somethingelse;
})
)
})
})
})
);
cacheCycle.then(resolveCacheProcessing); //for some reason it resolves before finishing
})
});
cacheProcessing.then((cacheProcessingResult)=>{
console.log("Done processing cache...")
})
My guess is this has something to do with the async operations of getting the cache keys and opening each individual cache. Any advice is greatly appreciated!