Promise.all()

Promise.all() A static method that accepts a list of promises and returns a promise that:

  • Resolved when all promises in the list resolved.
  • Rejected when any one of promises list rejected.
const firstPromise = Promise.resolve(1000);
const secondPromise = Promise.resolve("I am the second promise");
const thirdPromise = new Promise((resolve,reject)=>{
    setTimeout(()=>{
        resolve("After 1 second 3rd promise resolved :)");
    },1000)
});
Promise.all([firstPromise,secondPromise,thirdPromise]).then((data)=>{
    console.log(data);
});
//expected output: [1000,"I am the second promise","After 1 second 3rd promise resolved :)"]

If all the input promises resolve, the Promise.all() static method returns a new Promise that resolves to an array of resolved values from the input promises, in an iterator order.

const firstPromise = Promise.resolve(1000);
const secondPromise = Promise.resolve("I am the second promise");
const thirdPromise = new Promise((resolve,reject)=>{
        reject(new Error("I'm Rejected (:-"));
});
Promise.all([firstPromise,secondPromise,thirdPromise]).then((data)=>{
    console.log(data);
}).catch((err)=>{
    console.log(err.message)
});
//expected output: "I'm Rejected (:-"

If one of the input promises rejects, the Promise.all() returns a new Promise that rejects with the rejection reason from the first rejected promise. The subsequent rejections will not affect the rejection reason. The returned Promise also handles the rejections silently.

Promise.all() is useful when you want to aggregate the results from multiple asynchronous operations.

Promise.race()

Is a static function that accepts an array of promises but it returns the first settled promise what is meant by settled. Promise is rejected or resolved. Unlike promise.all (which accepts an array of promises, and will attempt to fulfill all of them. Exits early if just 1 promise gets rejected.)

First promise settled (resolved)

let firstPromise = () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(10);
      }, 1000);
    });
  };
  let secondPromise = () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(100);
      }, 2000);
    });
  };
  let thirdPromise = () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        reject();
      }, 3000);
    });
  };
  Promise.race([firstPromise(), secondPromise(), thirdPromise()])
    .then((data) => {
      console.log(data);
    })
    .catch((err) => {
      console.log(err);
    });
//expected output: 10

First promise settled (rejected)

let firstPromise = () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(10);
      }, 1000);
    });
  };
  let secondPromise = () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(100);
      }, 2000);
    });
  };
  let thirdPromise = () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        reject(800);
      }, 300);
    });
  };
  Promise.race([firstPromise(), secondPromise(), thirdPromise()])
    .then((data) => {
      console.log(data);
    })
    .catch((err) => {
      console.log(err);
    });
//expected output: 800 

Promise.race usage

The method name Promise.race is befitting because it causes all of the promises to “race” against each other with only a single winner.

Promise.any()

Takes array of promises as soon as one of the promises fullfill (resolved) its returns the first resolved promise. If no one of the given promises resolved (all promises rejected) its returns aggregateError.

const fisrtPromise = new Promise((resolve,reject) => {
    setTimeout(()=>{
        resolve("quick")
    }, 500)
});
const secondPromise = new Promise((resolve,reject) =>{
    setTimeout(()=>{
        resolve("slow")
    }, 1000)
});
const thirdPromise = new Promise((resolve,reject)=>{
    setTimeout(()=>{
        reject(new Error("Fast rejection"))
    },50)
}).then((err)=>{
    console.log(err)
})
const promises = [fisrtPromise, secondPromise, thirdPromise];
Promise.any(promises).then((value) => console.log(value));
//expected output: "quick"

To learn more about promises read this article Promises in JavaScript

Subscribe to our Newsletter

Subscribe to tech-hour website to get all updates, new articles, new courses and projects. No spam ever. Unsubscribe at any time.