From 2cf6fa2e9c06007dad18f86c0a7179efa3a9f6f9 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Fri, 8 Jan 2021 13:59:34 +0200 Subject: [PATCH] refactor(service-worker): avoid unnecessarily creating Promises in `Driver#deleteAllCaches()` (#40234) This commit refactors `Driver#deleteAllCaches()` to use `Array#map()` instead of `Array#reduce()` for running async operations in parallel. This allows avoiding having to recursively wrap Promises with `Promise.all()`. PR Close #40234 --- packages/service-worker/worker/src/driver.ts | 22 +++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/service-worker/worker/src/driver.ts b/packages/service-worker/worker/src/driver.ts index 106d3a45ca..bfb1a66861 100644 --- a/packages/service-worker/worker/src/driver.ts +++ b/packages/service-worker/worker/src/driver.ts @@ -722,18 +722,16 @@ export class Driver implements Debuggable, UpdateSource { } private async deleteAllCaches(): Promise { - await (await this.scope.caches.keys()) - // The Chrome debugger is not able to render the syntax properly when the - // code contains backticks. This is a known issue in Chrome and they have an - // open [issue](https://bugs.chromium.org/p/chromium/issues/detail?id=659515) for that. - // As a work-around for the time being, we can use \\ ` at the end of the line. - .filter(key => key.startsWith(`${this.adapter.cacheNamePrefix}:`)) // ` - .reduce(async (previous, key) => { - await Promise.all([ - previous, - this.scope.caches.delete(key), - ]); - }, Promise.resolve()); + const cacheNames = await this.scope.caches.keys(); + const ownCacheNames = + cacheNames + // The Chrome debugger is not able to render the syntax properly when the + // code contains backticks. This is a known issue in Chrome and they have an + // open [issue](https://bugs.chromium.org/p/chromium/issues/detail?id=659515) for that. + // As a work-around for the time being, we can use \\ ` at the end of the line. + .filter(name => name.startsWith(`${this.adapter.cacheNamePrefix}:`)); // ` + + await Promise.all(ownCacheNames.map(name => this.scope.caches.delete(name))); } /**