Skip to content

Instantly share code, notes, and snippets.

@jherax
Forked from davidbarral/promises.js
Last active July 16, 2024 17:17
Show Gist options
  • Save jherax/fd7b4c95a792baa85f876482a7616ce5 to your computer and use it in GitHub Desktop.
Save jherax/fd7b4c95a792baa85f876482a7616ce5 to your computer and use it in GitHub Desktop.
Promise.allSettled: polyfill
/**
* 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