Promise.race versus Promise.any
How it works
Promise.race settles with the first input settlement, whether fulfillment or rejection. Promise.any fulfills with the first fulfillment and rejects with AggregateError only when all inputs reject. An empty race remains pending; empty any rejects. Neither combinator cancels the remaining operations. A timeout implemented with race must separately stop the underlying work if that work supports cancellation.
Example 1
const inputs = [Promise.reject("first"), Promise.resolve("second")];Promise.race(inputs).catch(e => console.log("race", e));Promise.any(inputs).then(v => console.log("any", v));Expected output in the tested environment:
race firstany secondA rejection can win race, but any looks for fulfillment.
Example 2
Promise.any([Promise.reject("A"), Promise.reject("B")]).catch(e => console.log(e.name, e.errors.join(",")));Expected output in the tested environment:
AggregateError A,BThe aggregate contains all rejection reasons in input order.
Common mistake
Promise.race does not mean first success. Use any for that requirement.
Practice
Compare race and any for one rejected input followed by one fulfilled input; then test all inputs rejected.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Promise.race versus Promise.any — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Which combinator settles on the first rejection even if another input might succeed later?
What error type does Promise.any use when every input rejects?
What is the state of Promise.race([])?
For Promise.race versus Promise.any, what is the console output of example 1 in Node.js 22.16.0?
const inputs = [Promise.reject("first"), Promise.resolve("second")];Promise.race(inputs).catch(e => console.log("race", e));Promise.any(inputs).then(v => console.log("any", v));For Promise.race versus Promise.any, what is the console output of example 2 in Node.js 22.16.0?
Promise.any([Promise.reject("A"), Promise.reject("B")]).catch(e => console.log(e.name, e.errors.join(",")));