Implement Promise.allSettled From Scratch in JavaScript
Implement Promise.allSettled from scratch in JavaScript so it never rejects, reports each outcome as status, value or reason, and keeps input order.

Promise.allSettled(iterable) waits for every input to finish, whether it fulfills or rejects, and then fulfills with an array that describes each outcome. It never rejects because of the inputs. Each entry is an object with a status of "fulfilled" and a value, or "rejected" and a reason, and the entries are in the same order as the inputs. It was added in ES2020 to cover the case Promise.all cannot: running several independent jobs and collecting every result, successes and failures together. This post builds promiseAllSettled on Node.js 24 by changing the implementation of all in three small ways, shows the output, and covers the patterns that use the result, from splitting successes and failures to retrying only what failed. The siblings are Promise.all, Promise.race and Promise.any.
The behavior to match
const delay = (value, ms) => new Promise((resolve) => setTimeout(() => resolve(value), ms));
const fail = (message, ms) => new Promise((_, reject) => setTimeout(() => reject(new Error(message)), ms));
const results = await Promise.allSettled([delay("a", 20), fail("b failed", 5), 42, Promise.reject("plain reason")]);
for (const result of results) {
console.log(result.status, result.status === "fulfilled" ? result.value : String(result.reason));
}
console.log(await Promise.allSettled([]));fulfilled a
rejected Error: b failed
fulfilled 42
rejected plain reason
[]The slow success is first in the output because it is first in the input, even though the failure arrived earlier. The reason of a rejection is whatever the input rejected with: an Error for the second, a plain string for the fourth. The empty input gives an empty array at once, as with all. Nothing here rejects, and the call waits for all four inputs to finish.
The implementation
Three differences from all: both fulfillment and rejection are handled, each stores an outcome object, and nothing calls the outer reject.
function promiseAllSettled(iterable) {
return new Promise((resolve, reject) => {
if (iterable == null || typeof iterable[Symbol.iterator] !== "function") {
reject(new TypeError(`${String(iterable)} is not iterable`));
return;
}
const items = [...iterable];
if (items.length === 0) {
resolve([]);
return;
}
const outcomes = new Array(items.length);
let remaining = items.length;
const record = (index, outcome) => {
outcomes[index] = outcome;
remaining -= 1;
if (remaining === 0) {
resolve(outcomes);
}
};
items.forEach((item, index) => {
Promise.resolve(item).then(
(value) => record(index, { status: "fulfilled", value }),
(reason) => record(index, { status: "rejected", reason }),
);
});
});
}
const outcome = await promiseAllSettled([delay("a", 20), fail("b failed", 5), 42]);
console.log(outcome.map((o) => o.status));
console.log(outcome[1].reason.message);[ 'fulfilled', 'rejected', 'fulfilled' ]
b failedThe helper record keeps the bookkeeping in one place: store the outcome at its index, count down, and resolve when the count reaches zero. Because the second argument of then handles the rejection, the returned promise itself never rejects for an input failure. That also means no input produces an unhandled rejection, since every input has a rejection handler attached.
The objects are plain, with only the fields that belong to the status: a fulfilled outcome has value and no reason property at all, and a rejected one has reason and no value.
console.log(Object.keys(outcome[0]), Object.keys(outcome[1]));[ 'status', 'value' ] [ 'status', 'reason' ]Why it never rejects, and when it can
The only way for the returned promise to reject is a non-iterable argument, which is a programming error, and not an input failure.
try {
await promiseAllSettled(123);
} catch (error) {
console.log(error.name, "|", error.message);
}TypeError | 123 is not iterableThe message in the native function is the engine's own text, so a test should check the error type and not the words. Beyond that case, every input outcome, however bad, becomes an entry in the array.
Splitting successes and failures
The usual next step is to separate the two groups.
const settled = await promiseAllSettled([delay(1, 5), fail("x", 5), delay(3, 5), fail("y", 5)]);
const values = settled.filter((r) => r.status === "fulfilled").map((r) => r.value);
const errors = settled.filter((r) => r.status === "rejected").map((r) => r.reason.message);
console.log(values, errors);[ 1, 3 ] [ 'x', 'y' ]Because the order matches the input, you can also use the index to link a failure to its input, for example to know which URL failed.
const urls = ["/a", "/b", "/c"];
const fetched = await promiseAllSettled([delay("A", 5), fail("not found", 5), delay("C", 5)]);
const failedUrls = urls.filter((_, index) => fetched[index].status === "rejected");
console.log(failedUrls);[ '/b' ]This pairing by index is the main reason the order guarantee matters, and the reason an implementation that pushed results in completion order would be useless for this purpose.
Retrying only what failed
A pattern that builds on it: run a batch, find the failures, run those again, and stop after a few rounds.
async function runWithRetries(tasks, rounds = 3) {
const finalResults = new Array(tasks.length);
let pending = tasks.map((_, index) => index);
for (let round = 1; round <= rounds && pending.length > 0; round += 1) {
const outcomes = await promiseAllSettled(pending.map((index) => tasks[index](round)));
const stillPending = [];
outcomes.forEach((outcome, position) => {
const index = pending[position];
finalResults[index] = outcome;
if (outcome.status === "rejected") {
stillPending.push(index);
}
});
pending = stillPending;
}
return finalResults;
}
const tasks = [
async () => "always ok",
async (round) => {
if (round < 3) {
throw new Error(`fails in round ${round}`);
}
return "ok on round 3";
},
async () => {
throw new Error("never works");
},
];
const final = await runWithRetries(tasks);
console.log(final.map((r) => (r.status === "fulfilled" ? r.value : r.reason.message)));[ 'always ok', 'ok on round 3', 'never works' ]The first task succeeds in round one and is not run again. The second fails twice and succeeds in round three. The third fails in every round, and its last failure stays in the final results. The bookkeeping uses pending as a list of original indexes, so a result always lands in its own slot, whichever round produced it. This is the kind of code that allSettled makes short: the outcome objects carry the information about what to retry.
A version without it: the catch trick
Before allSettled existed, the same result came from catching each rejection and converting it to a value.
function settleOld(promise) {
return Promise.resolve(promise).then(
(value) => ({ status: "fulfilled", value }),
(reason) => ({ status: "rejected", reason }),
);
}
const old = await Promise.all([delay("x", 5), fail("y", 5)].map(settleOld));
console.log(old.map((o) => o.status));[ 'fulfilled', 'rejected' ]This is an equivalent way to write it: each input becomes a promise that never rejects, and Promise.all collects them. It is a good alternative to know, because it is how the function was done in libraries for years, and it is a one-line answer if an interviewer asks for the shortest correct version. The explicit implementation above is better when you want the loop to be visible.
Differences with all, race and any
The four combinators answer different questions, and mixing them up is a common source of bugs.
| Method | Fulfills when | Rejects when |
|---|---|---|
Promise.all | every input fulfills | the first input rejects |
Promise.allSettled | every input settles | never, for input failures |
Promise.race | the first input fulfills | the first input rejects |
Promise.any | the first input fulfills | every input rejects |
allSettled is the only one that always waits for everything, which is also why it can be slow: a single hung input keeps the whole result waiting. If an input can hang, wrap it with a timeout before passing it in, as shown in implementing Promise.race.
Reporting failures without losing them
The convenience of allSettled has a cost: a failure that nobody looks at is easy to forget. Because the returned promise fulfills, code that awaits it and ignores the array treats a total failure as success. Always inspect the outcomes. A small helper that throws an aggregate error when any entry was rejected keeps the benefit of seeing everything while restoring the failure signal for callers that need it.
For logging, record the reason with the input that caused it, using the index. An error message such as "request failed" is much more useful when it names the URL, the file or the user id. Keep the original Error object as well, since it carries the stack, and avoid turning it into a string too early.
Finally, be careful with memory in very large batches. The function holds one outcome object per input until every input is done, and each outcome keeps its value or reason alive. For a batch of hundreds of thousands of items, process them in chunks, call allSettled on each chunk and discard what you do not need, which also limits how many requests run at the same time.
Tests
import test from "node:test";
import assert from "node:assert/strict";
test("promiseAllSettled reports every outcome in order", async () => {
const result = await promiseAllSettled([delay("a", 10), fail("b", 1), 3]);
assert.deepEqual(result.map((r) => r.status), ["fulfilled", "rejected", "fulfilled"]);
assert.equal(result[0].value, "a");
assert.equal(result[1].reason.message, "b");
});
test("promiseAllSettled of nothing resolves to an empty array", async () => {
assert.deepEqual(await promiseAllSettled([]), []);
});ℹ tests 2
ℹ pass 2
ℹ fail 0How to present it
Start from all, since most interviewers expect you to know it, and say what changes: handle both outcomes and never call reject. Write the helper record, which removes the duplication between the two handlers, and use an index so the order is stable. Then give a use: separating successes from failures, or retrying the failures. If asked about the shortest version, show the settleOld mapping with Promise.all.


