Here you can see side by side.
I opened my Visual Studio Code editor and open my browser using Live server extension, and I already
created a HTML file named index.html.
First I'm going to create a script tag.
Inside the script tag I'm going to create a normal function.
Function test.
And this function going to return a string.
Hello world.
Let's call the test function.
And I want to call this function in my console console dot log inside the parentheses test.
Let's save the file.
You can see in my console it print.
Hello world.
There is nothing new here.
It's a basically a normal function.
Let's convert the function into a async function.
For this we need to type async keyword before the function keyword.
Now this is not a normal function.
This function only works on asynchronous mode means in our background.
And if I save this file, you can see in my console it returns a promise.
You can see my console.
Promise.
Result.
Hello, world.
Promise.
State fulfilled.
So now it's clear for you.
If we use async keyword before the function, it's return a promise and we already know we can use then
or catch function with promise.
Let's see.
So I'm going to comment out this line.
And then I'm going to call this function test.
In our async function we don't need to use resolve or reject function.
We can directly call then function.
And inside the then function I'm going to create an arrow function.
Whatever this function returns, and if it runs successfully, our then function call automatically
and inside the then function we can print our return value console dot log inside the parenthesis result,
and we need to create the result variable as parameter to store value result.
You can take any name and if I save this file you can see in my console it print hello world.
There is various method to create async function in JavaScript.
We can create async function with normal error function.
Let me show you.
First I am going to take a variable named taste.
Let taste equal to and this is our async function.
So this is another way to create async function.
If I save this file, you can see in my console it's written hello world.
It's mean.
It's worked properly.
And if I want to fit in in an arrow function, yes I can.
For this we do need to type function keyword.
And then I use equal to and greater than sign.
And you know if there is a single statement in our arrow function then we can delete this curly braces
and we can make it one liner.
And also we can remove this return keyword.
And if I save this file you can see it's worked properly.
So you can see how small our function has become.
Let's try to understand what is await method.