Let's talk about await method.
Await method.
Work inside the async method to run await method.
First we need to create async function.
As you can see on your screen, we create a async function named test, and inside the function I print
three statement.
If I call this function it going to print three of the statement one by one.
Suppose we have any code after this function.
Then it will print after that.
Let's talk about await method.
We can use await method in our async function.
Just we need to type await keyword before any of the code line.
Something like that.
The meaning of the line is I told my compiler to wait after printing log B what is the advantage of
this until the console process is completed?
Then the code outside of our async function start executing.
If I call the test function, first it going to print console A, then it come to await method.
And here I told my async function to wait it print console b.
After that, whatever code is there, it will not execute it.
So it jumps outside the test function and try to execute our other code.
And when all the other codes are completed, it will come back to await function and it will check whether
our work is done or not.
If our code is complete, then whatever code is there inside the async function, it will start execute.
So when we use await method in async function, it stop executing our function and jump outside the
function and try to execute other codes.
And when other codes are completed, it back to the await method and try to execute and try to execute
other async function code.
Mainly we use it when we want to fetch data from server, and we use it with our fetch method.
Using this fetch method, we can retrieve data from the server.
It may be Json data or text data.
The data we want to fetch from the server may take a while to arrive, and our external code continue
to run until the data came from the server.
When the complete data comes in, it goes back to await function again.
Then we continue to do the following task of our function.
So let's start the practical and see how we can use await method with our async function.