Promise Combinators
Promise combinators wait on several promises at once. all, allSettled, race, and any differ in when they settle and whether one failure aborts the group.
Detailed Explanation
You have three fetches, or a timeout versus a request. Combinators turn an array (or iterable) of promises into one promise.
Promise.all(iterable) fulfills with an array of values in input order when every input fulfills. If any input rejects, all rejects immediately with that reason. In-flight work is not cancelled unless you pass an AbortSignal into the underlying APIs. Empty input fulfills with [].
Promise.allSettled(iterable) waits for every input to settle and fulfills with { status, value } or { status, reason } per item. It does not reject for a failed child. Use it when you want a report, not a single success.
Promise.race(iterable) settles with the first child that settles, success or failure. A timeout is Promise.race([work, rejectAfter(ms)]). Empty race stays pending forever.
Promise.any(iterable) fulfills with the first fulfillment. Rejections are ignored until all have rejected, then it rejects with AggregateError. Empty any rejects immediately. Use it for "first mirror that works".
Inputs that are not promises are wrapped with Promise.resolve. The iterable is snapshotted; adding to the array later does not add a child.
What it costs you: leftover work. all rejecting on the first failure does not stop the other fetches. They still run, still parse, still retry, unless you abort. race with a timeout has the same leak: the slow request continues. Always pair combinators with AbortController when the work is cancellable.
Reach for all when every result is required. Reach for allSettled when you can tolerate partial failure. Reach for race for timeouts and first-event. Reach for any for first success among fallbacks.
Code Examples
const a = Promise.resolve(1);
const b = Promise.resolve(2);
Promise.all([a, b]).then((xs) => console.log(xs)); // => [ 1, 2 ]
Promise.all([a, Promise.reject(new Error("no"))]).catch((err) => {
console.log(err.message); // => no
});Promise.allSettled([a, Promise.reject(new Error("no"))]).then((rows) => {
console.log(rows[0]); // => { status: 'fulfilled', value: 1 }
console.log(rows[1].status); // => rejected
});function timeout(ms) {
return new Promise((_, reject) => {
setTimeout(() => reject(new Error("timeout")), ms);
});
}
function fetchWithTimeout(work, ms) {
const ac = new AbortController();
const raced = Promise.race([work(ac.signal), timeout(ms)]);
raced.catch(() => ac.abort());
return raced;
}Promise.any([
Promise.reject(new Error("a")),
Promise.resolve("ok"),
]).then((v) => console.log(v)); // => okCommon Gotchas
Rejection does not cancel siblings
abort them yourself or they keep running.
`Promise.race([])` never settles
you will hang. Guard empty lists.
`all` preserves order, not completion order
the array matches the input, even if the last promise finished first.
`any` wraps many errors in `AggregateError`
read `err.errors` for the list.