Promise.allSettled
- Version
- es2025
- Since
- es2020
- Review
- Draft
Summary#
Promise.allSettled waits until every input has fulfilled or rejected. It fulfills with one outcome object per input, in input order.
Syntax#
const outcomes = await Promise.allSettled([firstPromise, secondPromise]);
Example#
const outcomes = await Promise.allSettled([
Promise.resolve("saved"),
Promise.reject(new Error("offline")),
]);
for (const outcome of outcomes) {
if (outcome.status === "fulfilled") {
console.log(outcome.status, outcome.value);
} else {
console.log(outcome.status, outcome.reason.message);
}
}
Expected output#
fulfilled saved
rejected offline
Common mistakes#
A rejected input appears as a result with status: "rejected" and a reason, which can be any value. Check each status to find failures. A broken input iterable, such as one whose iterator throws, can still reject the combined promise.
Version applicability#
ES2020 added Promise.allSettled. The example uses ES2022 top-level await. Save it as an .mjs file to run it as a module in Node.js.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0