UNPKG

863 BJavaScriptView Raw
1import { AggregateError } from "./aggregate-error.js";
2export const isFulfilled = (r) => {
3 return r.status === 'fulfilled';
4};
5export const isRejected = (r) => {
6 return r.status === 'rejected';
7};
8/**
9 * Helper function that unwinds `Promise.allSettled`:
10 * Takes the promise returned and throws a `AggregateError` iff at least one promise settled with a rejection.
11 * Otherwise returns the list of fulfilled values.
12 * @param allSettledPromise A promise returned by `Promise.allSettled`
13 * @returns List of fulfilled values
14 */
15export const unsettle = async (allSettledPromise) => {
16 const rs = await allSettledPromise;
17 if (rs.every(isFulfilled))
18 return rs.map(r => r.value);
19 throw new AggregateError(rs.filter(isRejected).map(r => r.reason), "One or more Promises in 'unsettle' were rejected");
20};
21//# sourceMappingURL=unsettle.js.map
\No newline at end of file