What is the difference between Promise race and any methods with examples

A Promise is a JavaScript object utilized for asynchronous operations, facilitating the execution of future tasks with statuses:

  • pending: The default initial state
  • fulfilled: Signifying successful task completion with a value
  • rejected: Denoting task failure with a reason.

This feature was introduced since ES6 version in JavaScript.

Example of Promise.any() in JavaScript

The Promise.any() method accepts iterable promises and returns results as follows:

  • Resolves a promise when any one of the promises in the array is resolved.
  • Throws an AggregateError when all promises are rejected.
Promise.any(iterable promises)

Iterable promises are an array of promises.

It returns a resolved promise as soon as any one of the promises is resolved. If all promises are rejected, it throws an AggregateError: All promises were rejected.

Let’s explore different use cases.

  • Case 1: Multiple promises resolved

    When multiple promises are resolved, Promise.any() returns the first resolved promise.

    Promise.any([
      Promise.resolve("p1 success"),
      Promise.resolve("p2 success"),
      Promise.resolve("p3 success")
    ])
    .then((data) => console.log("result", data))
    .catch((error) => console.log("error", error))
    

    It returns the first resolved promise Output:

    result p1 success
    
  • Case 2: One of the promises resolved When one of the promises is resolved:

    Promise.any([
      Promise.reject("p1 failed"),
      Promise.resolve("p2 success"),
      Promise.reject("p3 failed")
    ])
    .then((data) => console.log("result", data))
    .catch((error) => console.log("error", error))
    

    Output:

    result p2 success
    
  • Case 3: All promises rejected

all method takes three promises. One promise is rejected, 2 processes are resolved and the result is failure and result is all promises are rejected.

Promise.any([
  Promise.reject("p1 failed"),
  Promise.reject("p2 failed"),
  Promise.reject("p3 failed")
])
.then((data) => console.log("result", data))
.catch((error) => console.log("error", error))

error
AggregateError: All promises were rejected

Example of Promise.race() in JavaScript

Promise.race accepts an iterable of promises and returns the result of the first promise that resolves or rejects.

When all promises are settled (either resolved or rejected), it returns an object:

If the promise is fulfilled, it returns an object with status=fulfilled and value. If the promise is rejected, it returns an object with status=rejected and reason.

Promise.all(iterable promises)
  • All promises resolved In this example, all promises are resolved:

      Promise.race([
      Promise.resolve("p1 success"),
      Promise.resolve("p2 success"),
      Promise.resolve("p3 success")
    ])
    .then((data) => console.log("result", data))
    .catch((error) => console.log("error", error))
    

    Output:

    result p1 success
    
  • One of the promises rejected In this example, one of the promises is rejected, but the remaining promises are resolved

    Promise.race([
    Promise.resolve("p1 success"),
    Promise.reject("p2 failed"),
    Promise.resolve("p3 success")
    ])
    .then((data) => console.log(data))
    .catch((error) => console.log("error", error))
    

    Output:

    p1 success
    
  • All promises rejected

    Promise.race([
    Promise.reject("p1 failed"),
    Promise.reject("p2 failed"),
    Promise.reject("p3 failed")
    ])
    .then((data) => console.log(data))
    .catch((error) => console.log("error", error))
    

    Output:

    error p1 failed
    

What is the difference between Promise race () and Promise any ()?

Promise racePromise any
Takes an iterable array of promisesTakes an iterable array of promises
Settles when any of the promises is fulfilled or rejectedSettles when any promise is resolved or all promises are rejected
Not parallelNot parallel