Promises in JavaScript

If you’ve ever sent a text to someone and kept checking your phone like it's about to explode… then congratulations my friend. You already understand Promises.
lets take a close look at it:
Promises
A Promise is an object that represents a value that will be available in the future.
in real life:
You: “please tell me if you're serious about this.”
Your_crush: “yeah, i'll tell you...”
That’s a Promise.
Three possible outcomes of Promises:
Pending -> Let’s see where this goes.
Fulfilled -> Yes, we're official.
Rejected → "It's not you" (tut gaya dil? nice its called "rejection")
here's a small example for this:
let relationship = new Promise((resolve, reject) => {
let vibeCheck = true;
if (vibeCheck) {
resolve("We are dating now");
} else {
reject("vibe match nhi huii");
}
});
relationship
.then(result => console.log(result))
.catch(error => console.log(error));
now in above code,
.resolve() - makes the promise successful.
.reject() - makes the promise fail.
.then() - executes if promise is fulfilled/succeed.
.catch() - executes if promise is rejected/failed.
drama right? aree aage aur bhi Hai...
let's learn about all the Promise types
Promise.all() - All or nothing.
"We’re only going if EVERYONE is coming." oh no.
Promise.all() waits for all promises to succeed.
If even ONE fails?
Whole promise failed.
let's learn this from a simple example:
so you guys are planning a trip so what require is to everyone says yes. but unfortunately, one of your friends says no.
toh kya hoga "trip cancel ho jayegi".
that's promise.all().
const rajiv = Promise.resolve("let's goo");
const crush = Promise.resolve("I'm in");
const akruti = Promise.reject(" akruti is not coming");
Promise.all([rajiv, crush, akruti])
.then(results => console.log(results))
.catch(err => console.log("Trip Cancelled:", err));
Output: Trip Cancelled : akruti is not coming.
this promise requires all promises to be fulfilled and if even one is failed. the whole thing fails.
Promise.allSettled() - Just updates
“Whatever happens, just give me some outcome.” this ones have no drama.
This waits for every promise to finish, even whether they succeeded or failed. It just wants a final status report from everyone.
let's learn this from example I've written below:
you texted 2 of your friends, so you just want report of their reactions, nothing else.
const promise1 = Promise.reject("left on read");
const promise2 = Promise.resolve("Friend replied");
Promise.allSettled([promise1, promise2])
.then(results => console.log(results));
Output:
[{status: "rejected", reason: "Left on read"},
{status: "fulfilled", value: "Friend replied"}]
You get:
Who ghosted (Promise1 ghosted)
Who replied (Promise2 replied)
You get full report that who did what.
Promise.race() - Real race it is.
“Whoever replies first wins.”
its like: "jo pehle reply karege use hi attention dunga. (ego toh dekho inka)"
this type returns the FIRST promise that settles. Promise.race() Doesn’t consider if it’s success or failure, only speed matters.
const slowReply = new Promise(resolve => setTimeout(() => resolve("Slow reply won"), 2000));
const fastReply = new Promise(resolve => setTimeout(() => resolve("Fast reply won"), 500));
Promise.race([slowReply, fastReply])
.then(result => console.log(result));
Output: Fast reply won
You text:
friend 1
friend 2
Whoever replies first gets your attention.
Promise.any() – I just need one good one.
"Bas ek sahi insaan mil jaye."
Promise.any() returns the first fulfilled promise. this ignores failures and waits for the first successful Promise. It only fails if every single Promise fails.
ye Rejections ko, red flag ignore karta hai, it needs a solid green flag to succeed.
let's see example:
so you tested 3 people, and only one of them is suitable for you, the green flag toh bas wahi enough hai.
const person1 = Promise.reject("red flag 1");
const person2 = Promise.reject("red flag 2 ");
const person3 = Promise.resolve("Green flag found");
Promise.any([person1, person2, person3])
.then(result => console.log(result))
.catch(err => console.log("All failed"));
Output: Green flag found
see, your promise ignored all the red flags, it only cares about the green ones.
Conclusion
Promise sorted? "ofc yaar kaise nhi hoga, Itna relatable jo tha", i know right..
just remember this shortcut while using promises into your code.
Promise.all() → all must succeed
Promise.allSettled() → get every result
Promise.race() → first one wins
Promise.any() → first success wins
Thank you for reading till end, I hope you understood how promises and its types work.




