Welcome back.
Let's see how we can make use of async await.
Great.
So back to Visual Studio code as usual.
Less common.
This one's out.
And now this one also lets comment out to avoid variable name conflicts and avoid unnecessary console
login.
So back here as usual, going to be async await.
Okay.
So now here we go.
All right.
So let's get started with the code.
So at the same logic, we are assuming we are going to make API call or write a code that needs some
time to execute to fetch a user.
So here the first thing is, let's see how we can create async function.
So let's follow the syntax here which says that we need to bring async in front of function name.
So here we go, async.
And then you bring in the function keyword and the function name is that.
So here is going to be function, the name of my function going to be get user and there you go.
So here what we are going to do is we are going to make API call to fetch a user.
So here make make API call to fetch user.
So here what you're going to do is that we can say that for the meantime, let's say the response to
our code and here we need to use await in front of any code that needs some time to execute.
So for this function, if we see that we put our weight in front of this function that needs some time
to execute.
So here we can also write our own function that needs some time to execute.
So our market as await.
And here I can write any code that needs some time to execute.
For the meantime, let's go ahead and create a function somewhere here and then we reference it inside
here.
So going to be normal function and then let's say make.
API request.
And then here we go, because this function needs some time to execute.
We need to return a performance from this particular function.
If you get back to a section where we talk about making red API calls, you better understand all these
concepts.
Again, we are going to repeat ourselves by using all these methods to code or to develop real world
applications.
So let's continue on.
So for this particular function is going to take some time to execute.
So after calling this function, we need to have with that.
So we're going to use a promise and set time out here to simulate async code.
So here we go, as we did before, we are going to return a function, a promise from this function
with some set timeout.
So here we go, make use of new and then the promise.
And then this promise are passed in the callback function.
I have the result.
And then the reject this one too.
We have done it many times, so that's what I'm speeding up here.
So for the meantime, let's inside the resolve here, we are going to simulate how we are going to get
a response.
So let's make use of set time out here to delay the response coming back.
So my error function is that in a time to finish executing, let's see, after 5 seconds, I want to
get the user back.
So here we are waiting to resolve the promise, resolve the promise, or we are going to reject.
So as we did before.
Let's assume that is user fetch going to be let me say is fetched.
Equal to trillion, and then the actual user led user is going to be objects with few property names,
the name going to be as Mickey and hence, let's say the age is equal to 20.
So let's make some conditions to return whether is resolve or rejects as we did before.
So for this one, if indeed is fetched, meaning we get back the response after 5 seconds, then we
can go ahead and then return result.
Then you pass in the user you want to send to the user and then if rejects, then we can also call reject
and then we can say that error fetching.
Fetch end user.
Perfect.
So now back to our async function now.
So for this one, like I said, if you're making API call this are going to be pretty simple.
We can use fetch to make the API call, get, get and then the end point, you're going to get very
simple as that.
But because we are not making API call, that's why I'm returning a promise from this one with some
time you needed to execute.
Then I kind of wait inside this function to get it.
So now I'm going to call the function here called make API request.
Remember, this function is returning.
What a promise.
And this one needs some time to execute.
Because look here, it needs 5 seconds to execute.
That's why we are waiting this particular function to get done before we can vote.
Send the response to the user.
So for this one, if I console.log the response here and then check the console.
Let's check it out.
Nothing has been displayed yet.
So if I call the function, get user.
So let's wait after 5 seconds.
One, two, three, four, five.
And indeed, you see that I get what, Mikki?
Here you see that we want to make use of dot then of the card to get a user, because I'm just using
one line of code, then I get it back without the then dot, then draw then.
So assuming we are making some API, call our life pretty easy.
Just typing in the API request here and we are good to go.
But the point here says that check it out.
It says that.
Come down here, use try and how to handle success and error.
So here we are making use of try and catch by default.
We are getting the result because indeed it has been resolved.
What about if it's rejects?
Let's say here is force, let's save it.
And now check it out after 5 seconds, if something's going to happen, you can see that we get the
same error as we did before.
But this time around we get our own error error fetching.
But this error is coming from JavaScript, meaning that anytime you see on code in promise is simply
means that we are not handling the error well.
So to do that, that's why we need the try and catch meaning catch is for error and try it for resolve.
Great.
So back to our function here.
Instead of doing this way, we are going to use try and try to handle that.
So here we bring in try and then press and here catch.
And then as a function call and here you're passing one agreements which present a value we get from
the reject for premise.
This one going to be catch.
Right.
So the same way as we use in promise.
Likewise in async await.
So here we are making the actual logic case or place it inside my trial and then catch just console.log
the the error here.
Now save it.
Check it out.
After 5 seconds, we're going to have the beautiful error that we implemented and indeed we have error
fetching user.
So inside this one to my try our console.log response now we let's now let's assume that our.
Core was success.
So true here and now after 5 seconds.
One, two, three, four, five.
And indeed, we have Mickey as our object.
So if this one looks clear to you, like I said, when we get to making API calls, this syntax is pretty
easy to use and I'm going to make use of using promise here and then use async await down here.
All right.
So the next video, I'm going to have our coding challenges and after that we move on to how we can
make API calls.
We are going to build awesome applications like weather applications, translation applications and
any API based project we can build using this concept.