So finally, I'm back to my Visual Studio Code editor, and you can see I create a HTML file name index
dot HTML.
And I also open this file in my browser using live server extension inside the head tag.
First I'm going to type script tag and inside the script tag.
First I'm going to take a variable late complete and its value is true.
It's a boolean value and I'm going to check this value in our promise method.
We will do our next code depending on this result.
Then I'm going to create a promise method.
So I'm going to take a variable named Pro.
And then I'm going to use new keyword new promise.
And inside this promise method we have to create a function and I type function keyword.
Then inside the curly braces we need to create conditions.
So I'm going to create if condition.
If inside the parentheses I am going to check our variable value.
So I type my variable name complete.
If true it redirect to if condition and if false then it redirect to else condition.
As I told you before, promise return only to result it's return reject or fulfill.
For this we need to take two parameter in our function.
For successful.
I am going to take result variable and for reject I'm going to take reject variable.
If our function is satisfied then inside the if condition I'm going to use resolve function and if reject
insert the else condition I'm going to call reject function.
We don't need to create this function, we just need to call this function.
If we call this function then we can use our inbuilt functions which is then and catch.
And now I'm going to send message for resolve condition.
I am successful for reject.
I am going to send another message.
I am failed and now I am going to call this Pro object in our console.
Console dot log inside the round Braces Pro.
It's not a normal function, it's an object.
So we don't need to use round braces after object name.
And if I save this file you can see in my console section you can see in my console it print.
I am successful because we sent true value in our complete variable.
And if I open it you can see promise result.
I am successful and promise state fulfilled.
But if I set false value to our variable and save this file, you can see a message I am failed and
also it's showing an error.
And if I open this promise, you can see promise state rejected.
Also we can send parameter like normal function.
So I'm going to remove this complete variable value.
And I'm going to create a function function pro inside the round braces complete.
Then inside the curly braces.
And I'm going to move this code inside the curly braces.
So our function going to return promise.
So for now we don't need to take variables.
So I'm going to remove this variable name and return new promise.
And we need to pass value when we call this function.
If I send true as an argument if I save this file you can see in my console it print I am successful.
And if I send false as an argument now it's print I am failed.
So our promise function worked properly.
And now I'm going to create two different functions for resolve and for reject.
For resolve I'm going to create a variable for late on fulfillment.
And I'm going to use arrow function.
And when we call this function I'm going to send a value which is result.
And this value came from resolve function inside the curly braces console dot log result.
Similarly we need to create another function for reject section.
So I duplicate this lines and replace on fulfillment with on rejection and send error as in parameter
console dot log print error.
And as I told you before, we need to use callback function to call this functions which are then end
catch.
But first we need to call pro function.
And inside the parentheses we need to send a value and our value is true dot.
Then if our.
Problem is resolved inside the parentheses.
We need to call on fulfillment function.
And I'm going to duplicate this line for rejection purpose.
And this time we need to use catch function.
And inside the parentheses we need to call on rejection function.
And for now I don't need this console.log section.
So I'm going to remove it.
If I save this file you can see in my console it print I am successful.
As I told you, promise has three stages.
First one is pending, then resolve and reject.
Let's talk about pending.
We use it before promise start.
So when I return the function inside the function first I'm going to type console dot log.
Please wait.
Now it is on pending stage.
And then I'm going to use set timeout function.
Set timeout.
And then I'm going to create arrow function.
I made a little mistake.
We have to write the pending stage inside the promise function.
So I am going to select this section and move it inside the promise method inside the set timeout section.
I'm going to pass this if condition.
And I want to pass this condition for 4000 milliseconds.
And if I save this file you can see in my console first it print.
Please wait.
And we need to wait for four second but then it print I am successful.
It print.
Please wait twice to resolve this problem.
I am going to use method chaining and make it.
Then in catch function one liner I am going to remove this catch function.
And after unfulfilment dot catch on rejection.
Now if result is true, it is called all fulfillment function and if false it call on rejection function.
And if I save this file you can see in my console section.
First it print.
Please wait.
So we need to wait for four second, then it print.
I am successful and similarly if I pass false parameter and save this file, we need to wait for four
second.
And then it print I am failed.
Please.
What is our pending message?
We use a timeout function because it is take a long time to came data from the server.
Let me show you one thing.
If you do not want to create this function, you can.
In that case, you directly call this function in then or catch section.
So I'm going to copy this result function and call it inside the then function.
And similarly I call error function in our catch function.
And I'm going to comment out this previous line.
And if I save this file first it run our pending stage.
Then it print.
I am failed because we send false value to our Pro function.
Not only that we can pass multiple argument to our Pro function.
For now, I don't need this line, so I'm going to remove it.
And inside my pro function I am going to pass two parameter.
Two and seven.
And inside my function I'm going to take two parameters x and y.
Then inside the promise I'm going to take another variable z.
Z equal to x.
Multiply by y.
Then insert the set timeout function.
If complete variable, replace with z.
If multiplication is possible, then insert the result function.
I'm going to use template string method inside the Backticks your result colon dollar sign inside the
curly braces.
Z means our result.
Similarly, inside the reject function I'm going to send wrong input.
If I save this file first we need to wait for four second.
Then it print our result 14 because we pass two numeric value.
So our condition is true.
But if we pass a string value then what happened.
So I'm going to pass a string value as an argument seven replace with small t.
And if I save this file now you can see in my console it's print wrong input.
Basically we handle errors using promise method.
I hope now you understand what is promise in advanced JavaScript and why and how we use it.
Thanks for watching this video.
See you on the next tutorial.