您需要使用
Promise.all
等待所有承诺:
return keys(cacheStore).then((keys) => { // 1st
return Promise.all(keys.map((key) => {
return this.getCachedResponse(key).then((item) => { // 2nd
if (item.expire < now) {
return this.deleteCache(key)
}
})
}));
})
使用
async
/
await
:
async invalidateCache() {
const now = Date.now()
const keys = await getKeys(cacheStore);
await Promise.all(keys.map(key => this.expireKey(now, key)));
}
async expireKey(time, key) {
const item = await this.getCachedResponse(key);
if(item.expire < time)
this.deleteCache(key);
}