-
-
Save jherax/fd7b4c95a792baa85f876482a7616ce5 to your computer and use it in GitHub Desktop.
Promise.allSettled: polyfill
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
/** | |
* The Promise.allSettled() method returns a promise that resolves | |
* after all of the given promises have either resolved or rejected, | |
* with an array of objects that each describe the outcome of each promise. | |
* | |
* For each outcome object, a `status` string is present. | |
* If the status is "fulfilled", then a `value` is present. | |
* If the status is "rejected", then a `reason` is present. | |
* The value (or reason) reflects what value each promise | |
* was fulfilled (or rejected) with. | |
*/ | |
if (!Promise.allSettled) { | |
Promise.allSettled = (promises) => | |
Promise.all( | |
promises.map((promise, i) => | |
promise | |
.then(value => ({ | |
status: "fulfilled", | |
value, | |
})) | |
.catch(reason => ({ | |
status: "rejected", | |
reason, | |
})) | |
) | |
); | |
} | |
// Usage | |
Promise.allSettled(promises).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment