Skip to content
the poor coder

await all pronmises

Poor Man
Published
Reading time
2 min read
On this page

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:#

Text

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:#

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC