Blog / Javascript

How to wait for all promises to resolve in JavaScript

How to wait for all promises to resolve in JavaScript

This post will cover how to wait for all promises to resolve in JavaScript rather than having to take them one by one when you have multiple promises that need resolving.

Will MaygerWill Mayger
March 21, 2022
Article

In JavaScript from time to time you might end up needing to wait for multiple promises/requests/async actions at a given time, if you just have one or two sequential calls then this might be easy enough to solve without much additional work.

But what happens when you have many or you don’t need or want them to be sequential? In this post we are going to look at how we can solve this problem with the use of Promise.all.

How to wait for all promises to resolve in JavaScript

To wait for all promises to resolve in JavaScript, you just need to add them into an array which can then be passed into the Promise.all method which will wait for all of the promises passed in to be resolved before carrying on.

Here is an example of how to use Promise.all:

const requestOne = fetch('example.com/1');
const requestTwo = fetch('example.com/2');
const requestThree = fetch('example.com/3');

const responses = await Promise.all([requestOne, requestTwo, requestThree]);
How to wait for all promises to resolve in JavaScript example

Alternatively instead of using the async/await syntax you could instead make use of promise.all().then like so:

const requestOne = fetch('example.com/1');
const requestTwo = fetch('example.com/2');
const requestThree = fetch('example.com/3');

Promise.all([requestOne, requestTwo, requestThree]).then(responses => {});

In the above examples we are creating three requests and storing them in variables, but you can notice that we are not waiting for the requests to resolve and we are simply storing the promises rather than the results.

We then pass these promises stored in our variables into the Promise.all method and await it to complete to get all the values once they have all been resolved.

Just to illustrate it further, if we were to do this sequentially, one by one, it would instead look like the following:

const requestOne = await fetch('example.com/1');
const requestTwo = await fetch('example.com/2');
const requestThree = await fetch('example.com/3');

Advantages and disadvantages of Promise.all

Both cases have their uses, advantages and disadvantages but to keep things simple one of the core benefits of making use of Promise.all is that you will be able to make multiple requests at once so that any given request does not block the others from loading.

What this means is that if the promises are heavy or slow by grouping them together you will only ever have to wait for the longest one rather than having to wait for all the requests individually because they will all resolve irrespective of each other.

For example, let’s say that we are using 3 requests, the first takes 5 seconds to resolve, the second takes 2 seconds to resolve, and the third takes 3 seconds to resolve.

By using Promise.all to resolve them all at once we will only have to wait for the longest request which will take 5 seconds.

However, using the same example, if we waited for each Promise to resolve sequentially, we would have a total waiting time of 10 seconds (request 1 + request 2 + request 3 = 5 seconds + 2 seconds + 3 seconds = 10 seconds), double that of using Promise.all.

Summary

There we have how to wait for all promises to resolve in JavaScript, if you want more like this be sure to check out some of my other posts!

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app

Some graphics used on this post were made using icons from flaticon.

Latest Posts

Learn React, JavaScript and TypeScript

Learn React, JavaScript and TypeScript

Join the platform that top tier companies are using.
Master the skills you need to succeed as a software engineer and take your career to the next level with Pluralsight.

Start here

Become an expert in ReactJS, TypeScript, and JavaScript.

Here you will find my personal recomendations to you, for full disclosure I earn a small commission from some of these links, but I only recommend what I trust and personally use.

Good things are coming, don't miss out!

Good things are coming, don't miss out!

Follow me on Twitter to stay up to date and learn frontend, React, JavaScript, and TypeScript tips and tricks!

Are you a novice, intermediate or expert react engineer?

Find out here by taking my fun, interactive, quick quiz which takes approximately 1 - 3 minutes. How well will you will do?

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app