Published Jul 27, 2021Updated Oct 8, 2022
Contribute to Docs

The .any() method iterates over an iterableObject of Promises …



The iterableObject is usually an array of Promise objects. If the array is empty, a Promise object that resolves into an empty array will be returned.

Example 1

If the iterable object is empty or all the Promises within are rejected, an AggregateError is thrown.

.then((values) => console.log(values))
.catch((err) => console.log(err)))// Output: [AggregateError: All promises were rejected]
.finally(() => console.log("Operations on Promise.any() have finished."))

Example #2

Below is an array of promises contains 3 Promise objects.

  • 2 of them, promiseA and promiseB, will be rejected.
  • The other one, promiseC, will fulfill and resolve with a message.

Using Promise.all, the value of the first successfully resolved Promise will be logged to the console.

const promiseA = new Promise((resolve, reject) => {
const promiseB = new Promise((resolve, reject) => {
reject('Always fails!');
const promiseC = new Promise((resolve, reject) => {
const promises = [promiseA, promiseB, promiseC];
.then((result) => {
console.log(result); // Output: Success!
.catch((err) => {
.finally(() => console.log('Operations on Promise.any() have finished.'));

All contributors

Looking to contribute?

Learn JavaScript on Codecademy