Understanding Promise.all : Running Multiple Promises at Once

Kiran PvKiran Pv
3 min read

Promise.all is a JavaScript method that lets you run multiple promises at the same time, instead of one after another. It takes an array of promises and returns a new promise that resolves when all the promises in the array have completed.

The Problem Promise.all Solves

When you're building apps, you often need to get data from different sources. For example, you might need to:

  • Get menu items from restaurants

  • Check availability of rides

  • See what entertainment options are available

Without Promise.all, you'd have to wait for each request to finish before starting the next one:

async function getEverything(city) {
  const restaurants = await Restaurant.find({ city: city });
  const homelyFood = await HomelyFood.find({ city: city });
  const rides = await Rides.find();
  const otherServices = await OtherService.find();
  const entertainment = await Entertainment.find();


  return {
    restaurants,
    homelyFood,
    rides,
    otherServices,
    entertainment,
    roomService
  };
}

This is slow because each request has to wait for the previous one to finish - even though they don't depend on each other!

How Promise.all Makes Things Better

Promise.all runs all these tasks at the same time (in parallel), making our app much faster:

async function getEverything(city) {
  const [restaurants, homelyFood, rides, otherServices, entertainment, roomService] = await Promise.all([
    Restaurant.find({ city: city }),
    HomelyFood.find({ city: city }),
    Rides.find(),
    OtherService.find(),
    Entertainment.find(),
    RoomService.find()
  ]);

  return {
    restaurants,
    homelyFood,
    rides,
    otherServices,
    entertainment, 
    roomService
  };
}

Comparing the Speed Difference

Let's say each request takes about 1 second:

  • Without Promise.all: 6 seconds total (1 + 1 + 1 + 1 + 1 + 1)

  • With Promise.all: Only about 1 second total (all requests happen at once!)

When to Use Promise.all

Use Promise.all when:

  • You need to make multiple async calls

  • The calls don't depend on each other's results

  • You need all the results before moving forward

When Not to Use Promise.all

Don't use Promise.all when:

  • Tasks need to happen one after another

  • You need the result from one task before starting another

  • You want to continue even if some tasks fail (in this case, consider Promise.allSettled instead)

How Promise.all Handles Errors

If any promise in the array fails (rejects), the entire Promise.all will fail immediately. It's like a chain - one break ruins the whole thing.

For example, if the Restaurant.find() call fails but all others succeed, your code will jump to the catch block without waiting for the other promises to finish.

Real-world Example Explained

Let's break down our code example:

const [restaurants, homelyFood, rides, otherServices, entertainment, roomService] = await Promise.all([
  Restaurant.find({ city: city }),
  HomelyFood.find({ city: city }),
  Rides.find(),
  OtherService.find(),
  Entertainment.find(),
  RoomService.find()
]);

This code:

  1. Creates an array with 6 database queries

  2. Runs all 6 queries at the same time using Promise.all

  3. Waits for all queries to finish with the await keyword

  4. Uses array destructuring to assign each result to a separate variable

  5. Continues running your code only after all data is available

Summary

Promise.all is a powerful tool that makes your code:

  • Faster by running tasks in parallel

  • Cleaner and easier to read

  • More efficient with resources

Instead of waiting for tasks one by one like standing in a long line, Promise.all lets tasks run simultaneously like multiple checkout lanes at a store - getting everything done much faster!

0
Subscribe to my newsletter

Read articles from Kiran Pv directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kiran Pv
Kiran Pv

Front-End Developer specializing in JavaScript and React.js. Enjoys reading, writing, and swimming.