JavaScript Promise.any() Method

In this article, we will see javascript Promise.any() method and how to use it, and what is the purpose of using it.

JavaScript promise object based on two code points, the one is producing code and another is consuming code. When producing code returns the result then it should have to call the two callback functions success() and error().

Let’s see the example of using the Promise.any() method in javascript.

Promise.any() Method

Promise.any() method is similar to Promise.race() method. It takes an array of promises as an input and returns a single promise.

The only difference is that the Promise.race() method will reject the flow if any of the promises fail but Promise.any() stores the first resolved promise regardless of other promises that have failed.

In the above code, we make three promises and make demo functions to return the result with setTimeout() function.

Then we passed the array of promises to any() method and return the single result with the first resolved promise.

Leave a Reply

Your email address will not be published. Required fields are marked *