Promise any()
Promise.any() is a JavaScript ES2021/ES2022 method that takes an array of Promises and returns the first resolved Promise, rejecting only if all Promises reject.
Syntax:
Promise.any(iter)
Example: This example, shows the basic use of Promise.any() method.
Javascript
let prom1 = new Promise((resolve, reject) => { reject( "Failure" ); }) let prom2 = new Promise((resolve, reject) => { reject( "Failed to load" ); }) let prom3 = new Promise((resolve, reject) => { resolve( "Worked" ); }) let prom4 = new Promise((resolve, reject) => { resolve( "Successful" ); }) let prom = [prom1, prom2, prom3, prom4]; Promise.any(prom).then((val) => { console.log(val) }); |
Output
Worked
ECMAScript 2021 (JS 2021/2022)
JavaScript in 2021/2022 continues to evolve, with ES2021/ES2022 bringing enhanced features like private class fields, promise improvements, and record data structures, boosting developer productivity and code quality.
Contact Us