Loading...
Suppose you have a plan to go to watch movies this afternoon. But you can't go to buy the ticket for that movie cause you have lots of work to be done.
So, you asked your friend to buy the ticket for you and inform you of the status of whether he bought it or not. So, you can decide on another plan if canceled.
In the meantime (Ticket Status = pending), you continue to finish your work. Even though you complete all other works, the plan's final status still depends on the status of ticket booking.
There are 2 possibilities you could expect.
Everything went well and your friend got the tickets. (Ticket Status = Resolved)
Theater got Housefull so, can't get tickets. (Ticket Status = Rejected) You wait for your friend's message. So, you can proceed further.
This is the exact same way promises work in Javascript. A promise is an object that returns a value that you hope to receive in the future. Example Code:-
var promise = new Promise(function(resolve, reject){
/** Producing Code May take some time**/
if(/* everything turned out fine /*){
resolve("Let's go to watch the movie")
} else {
reject(Error("make other plan"))
}
});
/** Consuming Code Must wait for a fulfilled Promise**/
promise.then(function(result)){
console.log(result)
}, function(err){
console.log(err)
}
Benefits of Promises:-
Thanks for reading this blog. And Promise me you will share this post your friends.