Promise.any: First to Succeed Wins
Learn how Promise.any returns the first fulfilled promise, ignores earlier rejections and throws an AggregateError only when every promise fails.

Promise.any asks a different question from its relatives. all asks whether everything succeeded, allSettled asks what happened to each one and race asks which one finished first. any asks which one succeeded first, and it treats failures as noise unless there is nothing but failure. That makes it the right tool for redundancy, such as several mirrors, replicas or fallback sources, where you only need one good answer. This post covers the behavior on Node.js 24 with printed output, the AggregateError that appears when everything fails, a worked mirror example, the empty-array case and when race is still the better choice. Promise.any is part of ES2021.
The behavior
The combined promise fulfills with the value of the first input to fulfill. Rejections before it are ignored.
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
const failAfter = (ms, message) =>
new Promise((_, reject) => setTimeout(() => reject(new Error(message)), ms));
const value = await Promise.any([
failAfter(5, "first source failed"),
delay(30, "slow success"),
delay(15, "fast success"),
]);
console.log(value);fast successThe 5 ms failure arrived first and changed nothing. With Promise.race, it would have won and rejected. With any, the combined promise kept waiting for a success and took the fastest one, at 15 ms.
When everything fails
If every input rejects, any rejects with an AggregateError. Its errors property holds every reason, in input order.
try {
await Promise.any([
Promise.reject(new Error("mirror a down")),
Promise.reject(new Error("mirror b down")),
Promise.reject(new Error("mirror c down")),
]);
} catch (error) {
console.log(error.constructor.name);
console.log(error.message);
console.log(error.errors.map((e) => e.message));
}AggregateError
All promises were rejected
[ 'mirror a down', 'mirror b down', 'mirror c down' ]The message text "All promises were rejected" is what V8 produces, and other engines word it differently, so branch on error instanceof AggregateError and read error.errors, never the message. Unlike Promise.all, no single reason is picked as the representative, because a lone error would hide the other failures.
Plain values and already settled promises
A non-promise value counts as fulfilled, so it wins.
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
console.log(await Promise.any([delay(20, "timer"), "plain"]));
console.log(await Promise.any([Promise.reject(new Error("x")), Promise.resolve("ok")]));plain
okThe empty array
Promise.any([]) rejects immediately with an AggregateError whose errors is empty, because there is no promise that could succeed. This is the opposite of Promise.race([]), which stays pending forever, and the opposite of Promise.all([]), which fulfills.
try {
await Promise.any([]);
} catch (error) {
console.log(error.constructor.name, error.errors.length);
}AggregateError 0If the list is built from data, a possibly empty list becomes a rejection, which is usually the signal you want.
Worked example: several mirrors
A download can come from any of three mirrors. We want the first good response and do not care which one it was.
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
const failAfter = (ms, message) =>
new Promise((_, reject) => setTimeout(() => reject(new Error(message)), ms));
function fromMirror(name, ms, ok) {
return ok ? delay(ms, { mirror: name, bytes: 1024 }) : failAfter(ms, `${name} unreachable`);
}
try {
const response = await Promise.any([
fromMirror("eu", 10, false),
fromMirror("us", 40, true),
fromMirror("asia", 25, true),
]);
console.log("served by", response.mirror);
} catch (error) {
console.log("all mirrors failed:", error.errors.map((e) => e.message));
}served by asiaThe eu mirror failed first and was ignored. Of the two successes, asia finished first. If all three had failed, the catch would list the three reasons, which is exactly what a person debugging an outage wants.
Cancelling the losers
Like race, any does not stop the other work. When the first success arrives, the slower requests keep going. For network calls, share an AbortController, and abort when any resolves.
const controller = new AbortController();
const attempt = (name, ms) =>
new Promise((resolve, reject) => {
const timer = setTimeout(() => resolve(name), ms);
controller.signal.addEventListener("abort", () => {
clearTimeout(timer);
reject(new Error(`${name} aborted`));
});
});
const winner = await Promise.any([attempt("a", 40), attempt("b", 10), attempt("c", 30)]);
controller.abort();
console.log(winner);bThe abort rejects the two losers, and since any has already settled, those rejections are handled internally and do not become unhandled rejections.
Pattern: a fallback chain with a preference
If you prefer one source but accept others, any does not express preference, because the fastest wins. For ordered fallback, loop instead.
async function firstSuccess(factories) {
const errors = [];
for (const make of factories) {
try {
return await make();
} catch (error) {
errors.push(error);
}
}
throw new AggregateError(errors, "all sources failed");
}
const value = await firstSuccess([
() => Promise.reject(new Error("cache miss")),
() => Promise.resolve("from database"),
() => Promise.resolve("from api"),
]);
console.log(value);from databaseThis tries sources one at a time, so it does not waste work, and it respects the order. Choose between the two by whether you value speed or preference and cost.
any versus race
| Situation | Use |
|---|---|
| First answer, success or failure, decides | Promise.race |
| First success decides; failures are tolerable | Promise.any |
| Timeout against real work | Promise.race |
| Redundant mirrors or replicas | Promise.any |
A timeout must use race, because the timer's rejection has to win. Using any for a timeout would ignore the timer's rejection until the real work also failed, and the timeout would never fire.
const slow = new Promise((resolve) => setTimeout(() => resolve("late"), 60));
const timeout = new Promise((_, reject) =>
setTimeout(() => reject(new Error("timeout")), 20),
);
console.log("any:", await Promise.any([slow, timeout]));any: lateWith any, the timeout was ignored and the slow work won, which is the opposite of what a timeout is for.
Support and TypeScript
Promise.any is available in all current browsers and in Node.js since version 15. In TypeScript, set lib to ES2021 or later so the type is known, and AggregateError comes with it.
Designing around redundancy
Promise.any shines when you have several equivalent ways to get the same answer, and the question to ask before using it is whether the answers really are equivalent. Mirrors of the same file are. Two replicas of a read-only database usually are. Two different services that might disagree about a price are not, and taking whichever answers first would make results depend on network luck.
There is also a cost to starting everything at once. Querying three mirrors triples the traffic for every request, and if each request is expensive, the redundancy can be more costly than the failures it protects against. A middle path is hedging: start the first request, and start a second only if the first has not answered within a short delay. That keeps the common case cheap and still bounds the slow case. You can build it by racing a delay against the first attempt, then calling Promise.any over the attempts that were started.
Whatever the design, report which source won. Knowing that most responses come from the same mirror tells you something about the others, and an AggregateError that appears in your logs tells you every source failed at once, which usually points at a shared cause such as your own network. Log the error list, not just the message.
Testing code that uses Promise.any
The two outcomes of Promise.any each need a test. For the success case, give the function several sources where the early ones fail and a later one succeeds, and assert that the caller gets the first success and nothing else. For the failure case, make every source fail, and assert that the function rejects with an AggregateError, that errors has one entry per source and that the entries are in input order. Add a test with an empty list of sources, since that case rejects immediately and surprises people. If the code is meant to cancel the losing requests, assert that each loser's signal was aborted after the winner arrived, using signals that record when they fire. Make sources controllable with Promise.withResolvers(), so each test decides who fails and who wins, and nothing depends on timing or luck. Keep each test small and named for the behavior it proves, such as "ignores early failures" or "rejects with every reason when all fail", so that a failing test tells the next reader what broke without opening the code.


