Promises - IN Relatable Mode !!!
What is a Promise?
So the story starts with 27th Jan, who joined a web dev cohort at Chai aur Code with a mind to get a job successfully in 6 months and he has only two options, either he will succeed or fail to get a job in six months. And in the same way, JavaScript has a Promise, where you make a promise in JavaScript which starts with Pending and it will be Pending till the web dev cohort is going on. The second state is called Resolved/Fulfilled where you get a job and your promise is fulfilled. And the third one is Rejected, where the heartbreak you deal with after so much hard work and you get rejected In the same way, a Promise in JavaScript is basically an object that holds the result of an asynchronous operation whether it succeeded or failed.
It has three states : Pending , fulfilled and rejected.
How we handle the Promise ?
The same way we handle all situations in our daily life. We have a successful way where it takes all the successful means Fulfilled and handles that outcome, just like getting a job offer and the happiness we feel we handle it the same way. It handles the Fulfilled things through then(). And the same way if some Rejected outcome comes, we handle that too, but now just like a wrapper we call it catch(), which deals with the Rejected things.
const promise = new Promise((resolve, reject) => {
let gotJob = false;
if (gotJob) {
resolve("MIl gyiiiiiiii");
} else {
reject("Kismat hi kharab hain");
}
});
promise
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
So .then() is the happiness moment where the promise is fulfilled like your first job and the .catch() is that wrapper when the promise is rejected it catches the rejection and handles the failure, this way we handle promise either succed or failure .
Three Types of Promises
Promise.all() :This is something every student can relate to Whenever we prepare for a job or an exam we try to succeed in every situation. But when reality hits and we get one setback in our life, whether it is a failure or a job rejection, some students get demotivated har man jaate hain they just don't want to go further anymore. Because from class 1 to 12 they never really faced rejection or failure so when they face it they say i am done, I don't want to go on. And in the same way, Promise.all() works exactly like this it goes nicely if it sees all resolving promises, but the moment it sees one rejection, it stops completely.
const class10 = new Promise((resolve) => resolve("Cleared Class 10"));
const class12 = new Promise((resolve) => resolve("Cleared Class 12"));
const jee = new Promise((resolve, reject) => reject("Failed JEE "));
const job = new Promise((resolve) => resolve("if u tried it"));
Promise.all([class10, class12, jee,job])
.then((results) => {
console.log(results);
})
.catch((error) => {
console.log(error);
});
Failed JEE
So when the JEE result comes and it fails it stops right there. It does not go further. Just like some students think life is over after one failure, Promise.all() does the exact same thing one rejection and it is done.
promise.allSettled():Promise.allSettled() is not like Promise.all(). It is more chill. I want to take Piyush Sir as an example. He failed many times in his life. Like Class 5 pass, Class 6 fail, Class 7 pass, Class 8 fail. And in college he got 6 backlogs. But he never stopped. Every time he failed, he learned from it and moved forward. This motivates us also that failure will come in life, rejection will come, but that is okay. Just keep going. So be like Promise.allSettled(), not like Promise.all()
const class5 = new Promise((resolve) => resolve("Class 5 Pass"));
const class6 = new Promise((resolve, reject) => reject("Class 6 Fail"));
const class7 = new Promise((resolve) => resolve("Class 7 Pass"));
const class8 = new Promise((resolve, reject) => reject("Class 8 Fail"));
Promise.allSettled([class5, class6, class7, class8])
.then((results) => {
console.log(results);
});
[
{ status: 'fulfilled', value: 'Class 5 Pass' },
{ status: 'rejected', reason: 'Class 6 Fail' },
{ status: 'fulfilled', value: 'Class 7 Pass ' },
{ status: 'rejected', reason: 'Class 8 Fail ' }
]
Promise.any(): It is like a philosopher student who just lives his life and does his work with no pressure. He thinks that just get one resolve and I am out of the race. It does not matter to him to go further. No rat race. He is just living his happy life. Same way Promise.any() works. It just waits for one promise to resolve and it is done. It does not care about the rest. Just get one success and it is out!
const job = new Promise((resolve) => resolve("Mil gyi JOb "));
const ceo = new Promise((resolve) => resolve("ceo ban skta hain "));
Promise.any([job, ceo])
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
Mil gyi JOb