Course contentsShow
← JavaScript
Lesson 1314 of 2,72329. PromisesFree lesson

Promise.any: First Fulfillment Wins

Learn how any resolves with the first successfully fulfilled promise in an array.

Promise.any: First Fulfillment Wins

You'll learn how Promise.any() resolves with the first promise that successfully fulfills, ignoring rejections along the way.

What It Does

Promise.any() takes an array of promises and returns a new promise that resolves as soon as any one of them fulfills. Unlike Promise.race() (which settles with the first promise to settle, whether fulfilled or rejected), Promise.any() waits for the first successful result. It only rejects if all promises in the array reject.

Think of it like trying multiple backup servers: you don't care which one responds first, as long as one succeeds. If all servers fail, then you get an error.

How It Works

const p1 = Promise.reject('Failed 1');
const p2 = Promise.reject('Failed 2');
const p3 = Promise.resolve('Success!');

Promise.any([p1, p2, p3])
  .then(result => console.log(result)); // "Success!"

Even though p1 and p2 reject, Promise.any() waits for the first fulfillment—p3.

When All Promises Fail

If every promise rejects, Promise.any() rejects with an AggregateError, which contains all the rejection reasons:

const allFail = Promise.any([
  Promise.reject('Error A'),
  Promise.reject('Error B')
]);

allFail.catch(err => {
  console.log(err.constructor.name); // "AggregateError"
  console.log(err.errors); // ["Error A", "Error B"]
});

Comparison with Promise.race

  • Promise.race(): First to settle (fulfill or reject)
  • Promise.any(): First to fulfill (ignores rejections unless all fail)

Key Takeaway: Promise.any() resolves with the first successful fulfillment and only rejects if all promises fail, making it ideal for "try multiple options, succeed with any" scenarios.