forked from GoogleChrome/workbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix potential unhandled rejections in StrategyHandler.doneWaiting
doneWaiting throws on the first rejected promise that it encounters. This means that subsequent rejected promises may result in unhandled rejection errors. Fixes GoogleChrome#3171
- Loading branch information
Showing
2 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export interface PromiseResolution<T> { | ||
status: 'fulfilled'; | ||
value: T; | ||
} | ||
|
||
export interface PromiseRejection { | ||
status: 'rejected'; | ||
reason: unknown; | ||
} | ||
|
||
export type PromiseResult<T> = PromiseResolution<T> | PromiseRejection; | ||
|
||
/** | ||
* Promise.allSettled polyfill based on | ||
* | ||
* https://github.com/es-shims/Promise.allSettled/blob/main/implementation.js | ||
* | ||
* which is (c) 2019 Jordan Harband and used under the terms of the MIT license. | ||
*/ | ||
export function allSettled<T>( | ||
iterable: Iterable<Promise<T>>, | ||
): Promise<PromiseResult<T>[]> { | ||
const values = Array.from(iterable); | ||
return Promise.all( | ||
values.map(function (item) { | ||
const onFulfill = function (value: T) { | ||
return {status: 'fulfilled' as const, value: value}; | ||
}; | ||
const onReject = function (reason: unknown) { | ||
return {status: 'rejected' as const, reason: reason}; | ||
}; | ||
const itemPromise = Promise.resolve(item); | ||
try { | ||
return itemPromise.then(onFulfill, onReject); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters