Created
July 3, 2019 12:56
-
-
Save christianscott/d0b58d80ea190978c6c7749247362993 to your computer and use it in GitHub Desktop.
Promise allSettled polyfill
This file contains hidden or 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
type SettleResult<T> = | |
| { status: 'fulfilled'; value: T } | |
| { status: 'rejected'; reason: any }; | |
function reflect<T>(promise: Promise<T>): Promise<SettleResult<T>> { | |
return promise.then( | |
value => ({ status: 'fulfilled', value }), | |
reason => ({ status: 'rejected', reason }), | |
) | |
} | |
function Promise_allSettled<T>( | |
promises: readonly Promise<T>[], | |
): Promise<readonly SettleResult<T>[]> { | |
return Promise.all(promises.map(reflect)) | |
} | |
export const allSettled = 'allSettled' in Promise | |
? (Promise as any).allSettled as typeof Promise_allSettled | |
: Promise_allSettled; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment