Last active
October 4, 2019 21:31
-
-
Save theverything/a22522a6170375956002421a07365a36 to your computer and use it in GitHub Desktop.
allSettled
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
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