Skip to content

Instantly share code, notes, and snippets.

@theverything
Last active October 4, 2019 21:31
Show Gist options
  • Save theverything/a22522a6170375956002421a07365a36 to your computer and use it in GitHub Desktop.
Save theverything/a22522a6170375956002421a07365a36 to your computer and use it in GitHub Desktop.
allSettled
interface FulfilledPromise<T> {
status: 'fulfilled';
value: T;
}
interface RejectedPromise {
status: 'rejected';
reason: string;
}
function allSettled<T>(
arr: Promise<T>[],
): Promise<(FulfilledPromise<T> | RejectedPromise)[]> {
return Promise.all(
arr.map(p =>
p.then(
(v: T) => ({ status: 'fulfilled', value: v }),
(e: Error) => ({ status: 'rejected', reason: e.message }),
),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment