JavaScript Promise and its States?

This video covers what are Promise in JavaScript and learn a bit about the different states of Promise.

Code

let add = (a, b) => {
let sum = a + b;
console.log("The calculated sum is ", sum)
return sum
}
setTimeout(() => add(3, 3), 5000)

Full Transcript

00:00 Hi there. Welcome to JavaScript series and I'm Deeksha Sharma from bonsaiilabs. Today's video will cover what are Promise in JavaScript and a bit about different States of Promise. So what is a Promise? A Promise is an object that is used as a place holder for results that it'll contain eventually sometime in the future. These results could possibly come from an asynchronous computation. Now your next question could be what are these eventual results that will come in the future? Let's say we have a function called add. This function adds two numbers provided as the input. It adds them prints the sum on the console and returns the sum back to the caller. If we call add() with two and three as argument, the function will immediately calculate the sum and return five to us. Now we wrapped this function in a setTimeout() call and provide three and three as arguments.

00:55 We also delay the execution of this function for five seconds. After at least five seconds. The result of six would be calculated. The result from timeout is known as eventual result because it doesn't compute immediately and rather had to wait for at least five seconds. So we can wrap those computations into a Promise object which will eventually provide the results. And how those eventual results are available? Great question! For that, we need to learn about the States of a Promise. At any given time, a Promise could be in a state. That state could be pending, which means that the eventual results are not yet available. It could also be in a fulfilled state, which means that an asynchronous computation is complete and the Promise object now has the results available. Finally, it could also be in a rejected state, which means that the asynchronous computation has failed and the result is an error.

01:52 So as you can imagine, when an asynchronous computation starts, the Promise is said to be in a pending state, it is when the computation either succeed, or it fails, the Promise change the state. It changes the state from pending to fulfilled if the computation was successful and change from pending to rejected in case of an error or unsuccessful computation. A Promise is said to be settled if it is either in fulfilled or rejected state, which means either the computation turned out to be a success and Promise has the results or there was an error during computation and the Promise has the reason of this failure. If it is settled, it cannot be in the pending state. A Promise is said to be resolved if it is settled or if it has locked in to match the state of another Promise, which means that the current Promise is settled but the other Promises on which it depends might still be pending. So here is a quick review of Promise and the States of a Promise. In the next video we'll work on some code problems related to Promise. I hope it was useful for you. If you haven't subscribed already, be sure to do that. And until then, my friends keep learning and share your knowledge. I'll see you on the next video.