Skip to main content

Command Palette

Search for a command to run...

Promise in JavaScript

Updated
β€’4 min read
Promise in JavaScript

What is Promise ? πŸ€”

First forget computer.

Promise matlab wada.

Jaise mummy bolti hai β€”
β€œPadhai karo, shaam ko pizza milega.”

Ab pizza mila ya nahi mila, wo baad ki baat hai.
But mummy ne future ka ek wada kiya.

πŸ‘‰ JavaScript me bhi Promise exactly same cheez hai.

It is a thing that says:

β€œAbhi result nahi hai, but future me milega… ya fail bhi ho sakta hai.”

Bas itna hi.

Real Life Example πŸ”

Suppose you ordered burger on Zomato.

Delivery boy ne bola:
β€œBhaiya 20 min me aa raha hu.”

Ab teen possibilities hai:

⏳ 1. Pending (abhi kuch pata nahi)

Delivery boy raste me hai.

Burger mila? β€” ❌
Cancel hua? β€” ❌

Bas wait hi wait.

Ye hota hai Pending state

βœ… 2. Fulfilled (burger aa gaya)

Door bell bajti hai πŸ””

Delivery boy:
β€œBhaiya burger le lo”

Ab wada pura ho gaya.

Ye hota hai Fulfilled

❌ 3. Rejected (burger nahi aaya)

Phone aata hai:

β€œSir rider gir gaya… order cancel .”

Ab burger nahi milega.

Ye hota hai Rejected

Important Baat 🧠

Promise hamesha 3 state me hi hota hai:

  • Pending β†’ wait chal raha

  • Fulfilled β†’ success

  • Rejected β†’ fail

Aur ek baar decide ho gaya, fir change nahi hota.

Burger aa gaya to aa gaya.
Cancel ho gaya to ho gaya.

Delivery boy bol nahi sakta:

β€œSir burger bhi le lo aur cancel bhi samajh lo.” πŸ˜‚

Ab Computer Wala Part (Darr mat) πŸ’»

Programing me kuch kaam time leta hai:

  • Internet se data lana

  • File download

  • Server se baat karna

Computer bolta hai:

β€œBhai tu apna kaam kar, result baad me dunga.”

Isliye Promise use hota hai.

Code Example (simple sa) πŸ‘Ά

const pizzaPromise = new Promise((resolve, reject) => {
  let shopOpen = true;

  if (shopOpen) {
    resolve("Pizza mil gaya πŸ•");
  } else {
    reject("Shop band tha 😭");
  }
});

Isme:

  • resolve = success

  • reject = fail

Result kaise milega ? πŸ€·β€β™‚οΈ

Computer khud nahi batata.

Tumko bolna padta hai:

πŸ‘‰ β€œResult aate hi bata dena.”

Uske liye .then() use hota hai.

pizzaPromise.then((msg) => {
  console.log(msg);
});

Agar success hua β†’ message print

Agar fail ho gaya ? 😬

Tab use hota hai .catch()

pizzaPromise
  .then((msg) => console.log(msg))
  .catch((err) => console.log(err));

Success β†’ then chalega
Fail β†’ catch chalega

Funny Example 🀣

Exam ka result Promise jaisa hota hai.

Result day se pehle:

πŸ‘‰ Pending β€” mummy bhi tension me, tum bhi

Result aa gaya:

Pass β†’ Fulfilled πŸŽ‰
Fail β†’ Rejected 😭

Aur sabse funny:

Result change nahi hota.

Teacher ko bolke:

β€œSir reject ko fulfill kar do please”
β€” nahi hota πŸ˜‚

One More Real Example 😏

Crush ko message bhejna:

β€œHi πŸ™‚β€

Reply nahi aaya β†’ Pending
Reply β€œHi” β†’ Fulfilled 😍
Reply β€œBhaiya bolu?” β†’ Rejected πŸ’€

πŸ”₯ Static Methods

Ab tak ek hi Promise tha.

Real life me ek sath bahut cheeze hoti hai.

Jaise Goa trip plan 😎

Promise.all() β€” Sab ayenge tabhi trip πŸš—

Tumne 4 dost ko bola Goa chalte hai.

Rule: Sab ayenge tabhi jayenge

Ek bhi mana β†’ trip cancel ❌

Promise.all([
  Promise.resolve("Rahul aa raha"),
  Promise.resolve("Aman aa raha"),
  Promise.reject("Kunal mana kar diya"),
  Promise.resolve("Rohit aa raha")
])
.then((data) => console.log("Trip pakki", data))
.catch(() => console.log("Trip cancel 😭"));

πŸ‘‰ Ek bhi reject β†’ sab fail

Bilkul real life jaisa πŸ˜…

Promise.any() β€” Koi ek aa jaye bas 😭

Ab tum desperate ho.

β€œBhai koi ek chal le mere sath”

Promise.any([
  Promise.reject("Rahul busy"),
  Promise.resolve("Aman aa raha"),
  Promise.reject("Kunal exam")
])
.then((data) => console.log("Trip ho gayi 😎", data))
.catch(() => console.log("Koi bhi nahi aya πŸ’€"));

πŸ‘‰ Ek success mila β†’ kaam ho gaya

Promise.allSettled() β€” Sabka result batao πŸ“‹

Ab tum sirf dekhna chahte ho:

Kaun aa raha
Kaun mana kar raha

Trip ho ya na ho, report chahiye.

Promise.allSettled([
  Promise.resolve("Rahul aa raha"),
  Promise.reject("Kunal mana"),
  Promise.resolve("Aman aa raha")
])
.then((data) => console.log(data));

Output type:

  • Fulfilled β†’ aa raha

  • Rejected β†’ mana

πŸ‘‰ Ye kabhi fail nahi hota
Bas sabka result de deta

Final Words 🫑

Agar aap non technical ho, ya programing nahi jante, to bas itna yaad rakho:

πŸ‘‰ Promise = Future ka wada

Success bhi ho sakta
Fail bhi ho sakta
Abhi wait chal raha hota

Bas JavaScript wale log isko fancy bana dete 😁

Agar ye samajh aa gaya to aap Promise concept samajh gaye.
Coding baad me bhi seekh sakte ho πŸ‘

Chalo ab chai piyo β˜•