-
Notifications
You must be signed in to change notification settings - Fork 1
/
unsettle.ts
21 lines (19 loc) · 979 Bytes
/
unsettle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { AggregateError } from "./aggregate-error.ts";
export const isFulfilled = <T>(r: PromiseSettledResult<T>): r is PromiseFulfilledResult<T> => {
return r.status === 'fulfilled';
}
export const isRejected = <T>(r: PromiseSettledResult<T>): r is PromiseRejectedResult => {
return r.status === 'rejected';
}
/**
* Helper function that unwinds `Promise.allSettled`:
* Takes the promise returned and throws a `AggregateError` iff at least one promise settled with a rejection.
* Otherwise returns the list of fulfilled values.
* @param allSettledPromise A promise returned by `Promise.allSettled`
* @returns List of fulfilled values
*/
export const unsettle = async <T>(allSettledPromise: Promise<PromiseSettledResult<T>[]>): Promise<T[]> => {
const rs = await allSettledPromise;
if (rs.every(isFulfilled)) return rs.map(r => r.value)
throw new AggregateError(rs.filter(isRejected).map(r => r.reason), "One or more Promises in 'unsettle' were rejected");
}