Hello, guys.
Good to see you back once again.
I'm back with a new tutorial related advanced JavaScript and in this tutorial we are going to learn
about promises.
Promise introduced in JavaScript ES6 version.
Let's try to understand promises with graphical images.
Suppose this is you and this is your friend.
Your friends calls you and says long time no see.
Let's meet.
And you replied, let's meet tomorrow.
It's mean.
You made a promise to your friend that you are going to meet him tomorrow.
And now we can define this promise in three stages.
Our first stage is pending.
Your promise will remain pending until tomorrow.
And if you meet your friend tomorrow, then you have fulfilled the promise.
This is the second stage of promise.
Then come the third stage, which is rejected.
If for any reason you get busy and you can't fulfill your promise, then you have to reject the promise.
So these are the three stages of promise pending, fulfilled and rejected.
Now the question is what we usually do based on promises.
You decide if you fulfill the promises, then you will go to the eat at the restaurant.
And if you reject the promise, then you will meet another day.
Both.
The results depend on your promise.
So at the same way JavaScript promise method works, we have a method named promise.
Then inside this promise, we specify a condition, and based on this condition it returned two results.
Otherwise resolve.
Otherwise reject.
And if our condition resolve now, we can use a new function called.
Then inside the then function, we are going to decide if our promise resolved.
Then what we are going to do.
As I told you, if you meet your friend tomorrow, then you will go to the restaurant.
And if our condition is rejected, then we call another function name catch.
Both the functions are callback function.
These are all inbuilt function from JavaScript, but you can take any function name for result function
and reject function.
It is up to you.
So this is the promise method which is introduced in JavaScript version.
Now let's talk about the syntax.
How we can use promise method in JavaScript.
First we need to take a variable.
Then in this variable we need to define a new promise.
Now this variable become object of promise.
And as I told you, we define a function inside this promise method.
So this is how we can do it.
Here we create a function inside this promise.
And also we can take arrow function.
Basically in this function here we need to pass two parameter in our case resolve and reject.
And you can take any name for this parameter.
If your promise is satisfied then you can call resolve function.
And if your promise failed then you can call reject function.
Now the question is how we can call the functions.
For that we can create a condition something like that.
As you can see in this promise here we create a if else condition.
You can set your own condition.
It is up to you.
And if your condition is fulfilled then you can call resolve function.
And if your condition fail, then inside the else part you can call reject function.
You don't need to create the function anywhere, just you need to call the function.
And as I told you, if your promise is resolve otherwise reject.
For the further work we can create another function in our case on fulfillment.
It's mean.
Whenever we call the resolve function, it's going to call this function.
And whatever resolve function return, we can store it in a variable in our case result.
And here you can take any variable name at the same way for respect.
Here we create another function which is on rejection.
And if this function is going to return anything then we can store it in our variable.
Then we are going to print it in our console.
And as I told you you can take any name for these variables.
Now these two function will not be called by itself.
These are not callback functions to call this two function.
JavaScript comes with some inbuilt function which is then and catch.
I already told about it.
These two functions are callback function.
If our condition resolved then it automatically call, then function and the then function call on fulfillment
function.
And if our condition is reject, then it's automatically call this callback function named catch.
And it's going to call this function on rejection.
So then function going to call on fulfillment function and catch function going to call on rejection
function.
So let's summarize the topic.
At first we are going to create a promise.
And inside the promise we are going to create a function.
And in this function here we need to take two variables.
Our first variable is for success and our second variable is for failure.
And then we create a condition inside this promise.
And if the condition is satisfied then it is going to call resolve.
And if the condition is failed then it's call reject.
And if resolve function call then automatically it's call this callback function name.
Then and if reject function call then it's automatically call the callback function named catch.
Now you might have question how we can send argument to this functions for our functions.
As you can see, we have parameters result and error.
So how we can send the value to this function whenever we call resolve.
Otherwise reject function.
With this function we send the value.
As you can see here is success.
Here is failure.
So whenever we call on fulfillment function this message is going to assign to this variable named result
at the same way it's worked for on rejection.
So this is it for this tutorial.
In the next tutorial we are going to start the practical.
So thanks for watching this video.
Stay tuned for the next tutorial.