Skip to main content

Command Palette

Search for a command to run...

Promise Methods Explained: CID Analogy

Updated
β€’4 min read

Pre Requisite for this blog

  • Basic JS Understanding honi chahiye bhai

  • Promises aane chahiye aapko.

  • Promises ke status pata hone chahiye.

What we'll study?

So, aaj hamlog study karne wale hain Promise ke methods ko yaani

  • Promise.all()

  • Promise.allSettled()

  • Promise.any()

  • Promise.race()

Isko ham real world analogy se samajhne wale hain toh sit back relaxed and have a chai.

CID Ka Case

Toh ji aaj ham log ko mila hai ek case jise ham sab solve karenge. Usmien hame ek bande Ramu par shak hai

Ham apne shak ko pakka karne ke liye 3 evidence bhejenge (Promise) Doctor salunkhe ke paas (zyadatar API) aur woh hame report wapis denge (Status & Data)

const fingerPrints = new Promise((resolve) => {
  setTimeout(() => resolve("finger print match hogya sir ji"), 2000);
});

const sketch = new Promise((resolve) => {
  setTimeout(() => resolve("Sketch ban gya apradhi ka"), 1000);
});

const footageCheck = new Promise((resolve) => {
  setTimeout(() => resolve("Ye pakda gya aur mat laga mask"), 1500);
});

1. Promise.all() - Sari reports match kare toh hamla

Is approach mien CID wale doctor salunkhe ke sare reports ke aane ka wait karenge

Promise.all([fingerPrints, sketch, footageCheck])
  .then((reports) => {
    console.log(`Salunkhe: ACP ji sari reports aagyi ${reports}`);
    console.log("ACP: Daya darwaja tod do");
  })
  .catch((error) => {
    console.log(`Salunkhe: Are yaar ramu apradhi nhi hai ${error}`);
  });

Bhai ye kya ho raha bhai:

  • Sab ka wait karenge (equality on top :)).

  • Agar ek ka status rejected toh fail

Problem:

  • Ek hi banda sab mien error daal sakta hai so agar aap ise use kar rahe ho toh Best of Luck... πŸ’€

Matalb agar galti se bhi ye hua toh:

const fingerPrints = new Promise((resolve, reject) => {
  setTimeout(() => reject("Match nhi hua"), 1000);
});

Toh fir game over... πŸ’€

2. Promise.allSettled() - Sabki report aane do

ACP ji bolte hain ki daya darwaja todne ke pehle sab ke liye ruk jao

Daya be like: "OK sir"

Promise.allSettled([fingerPrints, sketch, footageCheck])
  .then((results) => {
    console.log(results);
  });

Ismien output sabka aata hai:

[
 { status: "rejected", reason: "Salunkhe: match nhi hua toh kya karu?" },
 { status: "fulfilled", value: "Salunkhe: Ha mukhda toh same hai" },
 { status: "fulfilled", value: "Salunkhe: Pakda gaya chalu ban raha tha" }
]
// array return hoti hai isliye array hai mittar

3. Promise.race() - Joh first woh first

ACP chacha bolte hain ki ek saboot mil jaye isko toh aesa pakdunga ki zindagi bhar nhi bhoolega 😈

Promise.race([fingerPrints, sketch, footageCheck])
  .then((first) => {
    console.log("First Report:", fastest);
  });

Lekin isko use karte hue cautious raho becuase fastest request reject bhi ho sakti hai... Best of Luck πŸ’€

Agar Fulfill hui:

Aur agar Reject hua toh:

4. Promise.any() - Ek Report mili aur fielding set

Promise.any([fingerPrints, sketch, footageCheck])
  .then((result) => {
    console.log("Ek sabut mil gaya", result);
  })
  .catch(() => {
    console.log("Salunkhe ne rishwat li hai ek bhi saboot nhi mila");
  });

Yaha pe aap bas koi ek promise ke fulfill hone ka wait karte ho

Jese hi ek report aayi turant fielding set

sirf ek case hai jismien fail ho jaye joki hai ki sare promise fail ho jaye

Conclusion

  • Promise.all() - Ismien sare promise ke resolve hone ka wait karte hain agar koi ek bhi reject hua toh error.

  • Promise.allSettled() - Ismien bas sabke settle hone ka wait hota hai resolve ya reject se farak nhi padta.

  • Promise.race() - Joh fastest hoga wahi leke chalenge chahe Fulfill ho ya Reject.

  • Promise.any() - Bas ek fulfill hone ka wait fir toh fielding set kardenge.

Hope it helped you understand the topic clearly :)

JavaScript: Everything you should know

Part 1 of 26

This is your ultimate guide towards JavaScirpt, this series of articles include - JavaScript Operators: The Basics You Need to Know - Understanding Variables and Data Types in JavaScript - Template Literals in JavaScript - Control Flow in JavaScript: If, Else, and Switch Explained - Array Methods You Must Know - JavaScript Arrays 101 - Function Declaration vs Function Expression: What’s the Difference? - Arrow Functions in JavaScript: A Simpler Way to Write Functions - Understanding Objects in JavaScript - Spread vs Rest Operators in JavaScript - Destructuring in JavaScript - Map and Set in JavaScript - Understanding Object-Oriented Programming in JavaScript - Understanding the this Keyword in JavaScript - The new Keyword in JavaScript - The Magic of this, call(), apply(), and bind() in JavaScript - Array Flatten in JavaScript - String Polyfills and Common Interview Methods in JavaScript - Synchronous vs Asynchronous JavaScript - Callbacks in JavaScript: Why They Exist - JavaScript Promises Explained for Beginners - Promise Methods Explained: CID Analogy - Async/Await in JavaScript: Writing Cleaner Asynchronous Code - Error Handling in JavaScript: Try, Catch, Finally - JavaScript Modules: Import and Export Explained - The Part of Events You Never See The following 26 articles and topics are covered

Up next

JavaScript Operators: The Basics You Need to Know

What will we study? In this blog, we will study some topics related to JavaScript Operators. These are: What Operators are? Types of Operators The blog would be in chill language and tone so that

More from this blog

L

Learning Tech

61 posts

A Blog with dozens of articles on different langauges as well as frameworks, This isn't just for you to learn, i crafted it thinking that this could be my future reference as well

It covers Networking, Web Development, Mobile Developement and very soon GenAI too!