Skip to main content

Command Palette

Search for a command to run...

Handling Promises

Promise. all() | Promise.any() | Promise.allSettled()

Updated
•4 min read•View as Markdown
Handling Promises
S
I like to make complex technical topics easy.

Before reading this, i am assuming that you already know what promises are and if you don't you can visit my "All about promises" blog to understand this better.

Promise.all() , Promise.any() and Promise.allSettled() these all are widely used static method of JavaScript that takes an array of promises as an input but deals differently with respect to their behavior.

Understanding these methods will allow you to handle complex asynchronous operations.

Promise.all :

To understand Promise.all, let us take an basic example, think of it as like you are in school and having a group of four, participated in a dance competition and now each member of your group must be present at the day of competition for your group performance unless your group will be out from the competition. Similarly, like this Promise.all needs all the inputs to be resolved successfully or in technical way-

Promise.all method is used to handle multiple promises simultaneously and then wait for one of them to get resolve.

Promise.all returns a response only when all the promises are resolved.

#Code Example:

// 1. Create a few simple promises
const promise1 = Promise.resolve("Apple");
const promise2 = new Promise((resolve) => setTimeout(() => resolve("Banana"), 1000));
const promise3 = Promise.resolve("Cherry");

// 2. Use Promise.all to wait for all of them
Promise.all([promise1, promise2, promise3])
  .then((values) => {
    // This runs only after ALL promises finish
    console.log(values); // Output: ["Apple", "Banana", "Cherry"]
  })
  .catch((error) => {
    // This runs if ANY of them fail
    console.error("One of them failed!", error);
  });

Promise.any :

Keeping the above example in mind we can understand Promise.any very easily , think like before participating in the competition the members should get register their group first or say give their name to the head of the competition who is managing it. To get the group registered , if every member is present or not does not matter but only one person is needed to give name of the group. So similarly, Promise.any works, it needs at least one input to get resolved to return a response. Or technically -

Promise.any needs only one promise to get resolved from the input , It does not matters what happened to other of the inputs.

Promise.any returns the first promise that succeeds ignoring all the failures unless every single promise in the arrays fails.

#Code Example:

// 1. Create promises (the fastest one fails, the second one succeeds)
const promise1 = Promise.reject("Fail 1");
const promise2 = new Promise((resolve) => setTimeout(() => resolve("Winner!"), 1000));
const promise3 = Promise.resolve("Too slow!"); // This is actually faster, but promise2 is our focus

Promise.any([promise1, promise2])
  .then((value) => {
    // This runs as soon as the FIRST promise succeeds
    console.log(value); // Output: "Winner! "
  })
  .catch((error) => {
    // This ONLY runs if ALL promises in the array fail
    console.log("Everything failed:", error.errors);
  });

Promise.allSettled :

Going furthur with the same example, think the principle asked the head along with registering also take the attendance of the participants and so the presented member will get marked as present and other of them is marked as absent. So similarly, Promise.allSettled takes all the input and waits for all of them to get settled (either resolved or rejected) or in technical way -

The Promise.allSettled method is used to handle multiple promises at a time just like promise.all but waits for all of them to get settled before proceeding. It returns an array of objects as output along with outcomes of the input promises.

Promise.allSettled always returns an array of objects along with their status either resolved or rejected.

#Code Example:

// 1. Create a few promises (one will fail)
const promise1 = Promise.resolve("Apple");
const promise2 = new Promise((_, reject) => setTimeout(() => reject("Banana is bad!"), 1000));
const promise3 = Promise.resolve("Cherry");

// 2. Use Promise.allSettled to wait for everything, regardless of outcome
Promise.allSettled([promise1, promise2, promise3])
  .then((results) => {
    // This ALWAYS runs after all promises finish (settle)
    console.log(results);
    /* 
      Output: [
        { status: "fulfilled", value: "Apple" },
        { status: "rejected", reason: "Banana is bad!" },
        { status: "fulfilled", value: "Cherry" }
      ]
    */
  });
6 views