Skip to main content

Command Palette

Search for a command to run...

Promises in JavaScript

Updated
•5 min read•View as Markdown
Promises in JavaScript

Handling Multiple Promises in JavaScript – Hera Pheri Edition: “Kya Scene Hai Bhai!” 😂

Arre o Raju! Arre o Shyam! Arre o Babu Bhaiya!

Picture this: You are Raju from Hera Pheri. You three idiots (plus Jethalal who “joined for business”) have the perfect plan to become crorepatis tomorrow.

But for the plan to work, four critical things must happen in parallel:

  • Shyam will get the lawyer’s confirmation

  • You (Raju) will get the documents ready

  • Babu Bhaiya will bring the “investor”

  • Jethalal will bring the “funds from his shop”

Sab apne timeline pe chal rahe hain. Koi jaldi pahunch jaayega, koi “5 minute” bolega, koi seedha dhokha de dega.

Exactly like JavaScript jab tum multiple async tasks fire karte ho.

Ab yeh “plan” kaise sambhaloge?
Chalo, Hera Pheri + TMKOC + Welcome + 3 Idiots style mein samajhte hain!

Meet the Gang – Our Four Promises


const shyamPromise = new Promise((resolve) => {
  setTimeout(() => resolve("✅ Lawyer confirmed! Kya scene hai bhai!"), 1200);
});

const rajuPromise = new Promise((resolve) => {
  setTimeout(() => resolve("📄 Documents ready! Ab plan on track!"), 2800);
});

const babuPromise = new Promise((_, reject) => {
  setTimeout(() => reject("💔 Dhokha de diya Babu Bhaiya! Investor bhag gaya!"), 1800);
});

const jethalalPromise = new Promise((resolve) => {
  setTimeout(() => resolve("🛒 Funds laaya! Meeting ke beech call aa gaya tha sorry!"), 3500);
});

1. Promise.all() – “Sab Ready Hona Chahiye Warna Plan Cancel!”

Jaise movie mein – sab kuch perfect chahiye. Ek bhi gadbad → pura plan flop!

Promise.all([shyamPromise, rajuPromise, babuPromise, jethalalPromise])
  .then(results => {
    console.log("🎉 Kya scene hai bhai! Poora plan set!");
    console.log(results);
  })
  .catch(err => {
    console.log("💥 POLICE AA RAHI HAI BHAI! Plan CANCEL!");
    console.log("Sab ghar jaao… Maggi khaao 😭");
  });

Babu Bhaiya ne dhokha diya? Turant reject.
Use when: Payment + order + email – sab ek saath perfect chahiye.

2. Promise.allSettled() – “All Izz Well!”

Real dosti mein ek dhokha se plan nahi toot ta. Report card toh chahiye na!

Promise.allSettled([shyamPromise, rajuPromise, babuPromise, jethalalPromise])
  .then(results => {
    console.log("📋 Final Report Babu Bhaiya:");
    results.forEach(r => console.log(r.status, r.value || r.reason));

    console.log("💪 Ek ne dhokha diya… baaki teen hain na?");
    console.log("

<grok-card data-id="e4dca9" data-type="image_card" data-plain-type="render_searched_image"  data-arg-image_id="RXYj5"  data-arg-size="LARGE" ></grok-card>

 ALL IZZ WELL!");
  });

Sabka full status milega – kaun aaya, kaun dhokha diya. Phir bhi plan adjust kar lenge.
Real projects mein yeh sabse best hai – dashboards, uploads, analytics.

3. Promise.race() – “Jo Pehle Pahunche, Usske Saath Shuru!”

Babu Bhaiya late? Koi baat nahi – jo pehle ready, usi se shuru karo!

JavaScript

Promise.race([shyamPromise, rajuPromise, babuPromise, jethalalPromise])
  .then(first => {
    console.log("🏆 Kya scene hai! Pehla ready:", first);
    console.log("Chalo bhai, plan shuru karte hain!");
  });

Shyam sabse fast (1200ms) → meeting shuru!
Use when: Fastest API, live updates, timeout.

Danger: Agar fastest wala dhokha de de toh pura race reject!

4. Promise.any() – “Maine Kaha Na… Ek Hi Kaafi Hai!”

Ek bhi sahi ho toh plan chalega bhai!

Promise.any([shyamPromise, rajuPromise, babuPromise, jethalalPromise])
  .then(hero => {
    console.log("✅ Koi ek toh aa gaya! Maine kaha na!");
    console.log(hero);
  })
  .catch(() => {
    console.log("💀 Sab ne dhokha de diya… ab kya karein?");
  });

Sirf ek success → party on. Sab reject → tab roye.
Perfect for: Fallback APIs, multiple payment gateways, “koi ek investor mil jaaye”.

Bonus: The Emotional async/await Scene

JavaScript

async function startPlan() {
  try {
    const report = await Promise.allSettled([shyamPromise, rajuPromise, babuPromise, jethalalPromise]);
    console.log("📋 Final Status:", report);
    console.log("Dosti mein no sorry, no thank you… bas ALL IZZ WELL! ❤️");
  } catch (err) {
    console.log("Kuch toh gadbad hai daya…");
  }
}

startPlan();

Quick Meme Cheat Sheet (Laga lo apne monitor pe)

Method Hera Pheri Vibe Kab Use Karo
all() “Police aa rahi hai!” Sab kuch perfect chahiye
allSettled() “All Izz Well!” Real life – adjust kar lenge
race() “Jo pehle ready, uske saath shuru” Speed matters
any() “Maine kaha na… ek hi kaafi hai!” Fallbacks & jugaad

Last Scene – Chai ki Tapri pe

Babu Bhaiya ne dhokha diya tha… phir bhi teeno (chaar) dost has rahe hain. Plan thoda badal gaya, lekin dosti nahi badli.

Life bhi Promises jaisi hi hai yaar… kabhi dhokha, kabhi “All Izz Well”, kabhi “Kya scene hai bhai!”

Lekin jo sachche dost hote hain, unpe hamesha bharosa rakho – code mein bhi, zindagi mein bhi.

Master these four methods and your async code kabhi “dhokha” nahi dega.

Ab jaao… apna next feature mein yeh lagaao aur comment mein batao –

Tumhara sabse bada “Babu Bhaiya dhokha” kab hua? 😂

Tag your coding dosts jo hamesha “All Izz Well” karte hain!

Happy coding, happy planning!
Kya scene hai bhai… ab code likho! 💰❤️