await all pronmises

Awaiting All Promises

When we write asynchronous code in JavaScript, we use promises to handle the results. Promises help us to write better code that is easier to read and maintain. Promises can be in different states, such as:

  • Pending: The promise is neither fulfilled nor rejected.
  • Fulfilled: The promise is fulfilled and returns a value.
  • Rejected: The promise is rejected and returns a reason for rejection.

When we have multiple promises, we can use the Promise.all() method to wait for all of them to be fulfilled or rejected. This method takes an array of promises and returns a new promise that resolves when all of the promises in the array have resolved, or rejects as soon as one of the promises rejects.

Example:


const promise1 = new Promise((resolve, reject) => {
   setTimeout(() => {
      resolve('Promise 1 resolved');
   }, 1000);
});

const promise2 = new Promise((resolve, reject) => {
   setTimeout(() => {
      resolve('Promise 2 resolved');
   }, 2000);
});

const promise3 = new Promise((resolve, reject) => {
   setTimeout(() => {
      reject('Promise 3 rejected');
   }, 3000);
});

Promise.all([promise1, promise2, promise3])
   .then(values => {
      console.log(values);
   })
   .catch(error => {
      console.log(error);
   });

In the above example, we have three promises with different timeouts. Promise 1 resolves after 1 second, promise 2 resolves after 2 seconds, and promise 3 rejects after 3 seconds. We use Promise.all() to wait for all three promises to be fulfilled or rejected. The .then() method will only execute when all three promises have resolved, and it will return an array of values returned by the promises. The .catch() method will execute as soon as one of the promises is rejected, and it will return the reason for rejection.

We can also use Promise.allSettled() method which is similar to Promise.all() but it resolves when all of the promises in the iterable argument have been settled, i.e., fulfilled or rejected.

Example:


const promise1 = new Promise((resolve, reject) => {
   setTimeout(() => {
      resolve('Promise 1 resolved');
   }, 1000);
});

const promise2 = new Promise((resolve, reject) => {
   setTimeout(() => {
      resolve('Promise 2 resolved');
   }, 2000);
});

const promise3 = new Promise((resolve, reject) => {
   setTimeout(() => {
      reject('Promise 3 rejected');
   }, 3000);
});

Promise.allSettled([promise1, promise2, promise3])
   .then(values => {
      console.log(values);
   });

In the above example, we have used Promise.allSettled() method which returns a new promise that resolves with an array of objects, each object representing the outcome of each promise in the iterable argument. The object has a status property that is either "fulfilled" or "rejected" and a value property that contains the fulfillment value or rejection reason of the promise.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe