Skip to main content

Command Palette

Search for a command to run...

Handling Multiple Promises in JavaScript – Promise.all, Promise.race & Promise.any Explained with War Room Analogy

Updated
3 min read
Handling Multiple Promises in JavaScript – Promise.all, Promise.race & Promise.any Explained with War Room Analogy

“War room mein decision tab hota hai jab sahi reports sahi time par milti hain…
Exactly waise hi JavaScript mein async operations handle hote hain.”

Scene: Modern War Control Room

Border tension chal rahi hai.

HQ ko 3 alag units se update chahiye:

  • Satellite Surveillance

  • Drone Strike Unit

  • Intelligence Team

Teen independent operations.
Teen lag timing.
Exactly like three asynchronous Promises.

Step 1: Three Missions = Three Promises

const satellite = new Promise((resolve) => {
  setTimeout(() => resolve("Satellite: Enemy Located"), 2000);
});

const drone = new Promise((resolve) => {
  setTimeout(() => resolve("Drone: Target Locked"), 1000);
});

const intel = new Promise((resolve) => {
  setTimeout(() => resolve("Intel: Strategy Ready"), 1500);
});

Har unit independently kaam kar rahi hai.
HQ wait kar raha hai.

1. Promise.all() – Sab Report Aane Do

Commander bolta hai:
“Jab tak teenon report nahi aati, koi action nahi.”

Promise.all([satellite, drone, intel])
  .then((reports) => {
    console.log("All Reports Received:", reports);
    console.log("Commander: Execute Operation");
  })
  .catch((error) => {
    console.log("Mission Failed:", error);
  });

Behavior:

  • Sab fulfill → Operation start

  • Ek bhi reject → pura mission fail

High dependency = High risk.

When One Mission Fails

const drone = new Promise((resolve, reject) => {
  setTimeout(() => reject("Drone Shot Down"), 1000);
});

Ab kya hoga?

Promise.all() immediately reject
Commander: “Abort Mission!”

2.Promise.allSettled() – Mujhe Sabka Status Chahiye

Kabhi-kabhi HQ ko sabka result chahiye — chahe fail ho ya pass.

Promise.allSettled([satellite, drone, intel])
  .then((results) => {
    console.log(results);
  });

Output:

[
 { status: "fulfilled", value: "Satellite: Enemy Located" },
 { status: "rejected", reason: "Drone Shot Down" },
 { status: "fulfilled", value: "Intel: Strategy Ready" }
]

Yeh analysis mode hai.
Decision baad mein hoga.

3.Promise.race() – Jo Pehle Confirm Kare

Emergency situation.
“Jo pehle confirm karega, uspar action lenge.”

Promise.race([satellite, drone, intel])
  .then((fastest) => {
    console.log("Fastest Update:", fastest);
  });

Speed matters more than coordination.

4.Promise.any() – “Ek Success Kaafi Hai”

Promise.any([satellite, drone, intel])
  .then((result) => {
    console.log("At least one success:", result);
  })
  .catch(() => {
    console.log("All missions failed");
  });

Behavior:

  • Ek bhi fulfill → Success

  • Sab reject → Error

Backup strategy activated.

Async/Await – Clean Command Structure

async function warRoom() {
  try {
    const reports = await Promise.all([satellite, drone, intel]);
    console.log("All Clear:", reports);
  } catch (error) {
    console.log("Critical Failure:", error);
  }
}

warRoom();

Readable. Structured. Professional.