.any()
Published Jul 27, 2021Updated Oct 8, 2022
Contribute to Docs
The .any()
method iterates over an iterableObject
of Promises …
Syntax
Promise.any(iterableObject);
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.
Promise.any([]).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
andpromiseB
, 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) => {reject(0);});const promiseB = new Promise((resolve, reject) => {reject('Always fails!');});const promiseC = new Promise((resolve, reject) => {resolve('Success!');});const promises = [promiseA, promiseB, promiseC];Promise.any(promises).then((result) => {console.log(result); // Output: Success!}).catch((err) => {console.log(err);}).finally(() => console.log('Operations on Promise.any() have finished.'));
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.