Welcome back, guys, to another lecture today.
We're going to talk about async a wait, and this is actually one of the most important features in
years.
Eight now async.
Oh, wait, it's nothing to do with how we write promises is actually how we consume them.
So I'm going to show you that.
Go back to this jump to this URL, Jason Placeholder and type type II diabetes dotcom.
Right.
Let me just grab this one.
Go back to Google here.
Right.
And search this one, Sanjay.
Someplace cooler type code dot com.
Now, here here is a lot of users who actually have search this keyword before us.
OK, now from here, I just want to grab the users and you can go through the examples and documentation
and but I will need this one later, so I'm going to copy it right now.
But I want to show you, if you go forward slash users, it will give me a Jason here screen with a
lot of things inside.
So the ID, a name, a user name and email for at least 10 users.
Right.
OK, so I'm going to be what we have here instead of to DOS because they have a lot of examples there.
I'm going to grab the users and this is how we change the then method.
And what async is doing is making these synthetical shuger how we consume the promise.
Now the mostly benefit for async away is that this function will run in the background, so it will
run in the background.
Like I'm just going to copy this one and I'm going to go to the new Dev.
I'm going to go to the inspector here and the console and I'm going to paste this one.
So look, first, when I click here, the promise will be pending and after that it will be executed.
Here it is.
It was first the initial state spending, the promise.
So there's always going to return a promise regardless of the output.
And here we have the output here is successfully resolved means if it was not successful, it would
have been rejected.
No, cuz now I have 10 arrays of 10 objects.
I have arrays of 10 objects, actually.
Right.
And so in initial state it was spending.
But this is how we have right.
Without the async away.
So the beauty of wasting away is that that function actually can work in the background, can wait for
this because we have a wait keyword inside.
Now we wait for the promise to be resolved.
Doesn't matter if it's going to be rejected or not.
So it's going to wait for that result.
But everything after it is, for example, imagine that we here we have a code here underneath we have
a code and this is the single way function.
So this code will run synchronously.
So the async awake will not block our code from working.
So it will run in the background until it gets the result.
Okay, so let's rewrite this one using the async away.
So async must we need you to use that keyword?
And after that we will see a function and we will say fetch the user's right, make sure that its users.
Now inside here I am going to say I'm going to copy first things first this.
And I'm not going to use the then method here right now.
As I told you, every single asynchronously function have a keyword of a weight now.
So this program will we actually will run in the background, but it will wait until this promise is
resolved.
And when it's resolved, we usually const result just put it back to the result if I can type it.
OK, now I do have the results stored in this variable here so I can see another function const data
and I will say again, I wait because this data, for example, it will go here and it will say, OK,
I'm waiting for it.
So let's jump to this line.
So and this line is still waiting for the result of this.
So now what is so nice with Jason?
I'm sorry, we need to use the Jason method here.
Well, it's so nice because actually we say, OK, this data is required to result from the previous
promise so it will not run and so it will skip until we get that one.
So it will not block our code.
And if the promise is not there, it will go above this below this line and we will execute synchronously
the rest of the code.
So that is the beauty.
So simple as that async function allows this function to work in the background until it gets the result
and it will not get blocked.
Because this one, this line is.
Waiting for the results of this line and this line is still waiting, for example, then you will say,
OK, he's still waiting for the result, we're not going to throw in there because you're trying to
get that call.
Now, the most usual way to do this place, again, to use the console, lock the lock and put the data
back.
Now, one more thing that we need to call this function, because it's not going to work so that users
outside here.
So I'm going to copy the entire one here and I'm going to have the results.
Same result as before.
So I'm going to just enter.
And here it is.
The result was the promise was pending and I got the same same results, same error.
If I go back here is well, Leon Graham and here is Leon.
Right.
Username Brett.
Nice.
So now you know why we need to use the async function.
And it's like a synthetic shuga, how we consume the promise.
This is more readable than this for me.
I use then method, then I change another method.
Imagine if we have a couple of steps here.
We need to change a couple of things.
Right.
And in the previous lecture, just in the previous lecture, I used different examples so you can go
and see this one.
But it's pretty much this.
So it's a synthetic shuger of how we doing things.
And it's make more simple easier to get graphs.
And yeah.
So it is not less code, by the way.
It's pretty much like the same amount of code, but it's a four i's more pleasing.
So somebody will argue, OK, this is a lot more than this.
Well it is, but this is the way how we should get used to writing the asynchronous function and that's
why we use them in our previous project.
So whenever we deal with APIs, it's best to use it like this.
Right.
So that is pretty much it for this one.
And I will see you in the next one.
By the way, we you do have the essay exercise.
So if I click here now, look through this one.
It's nothing scary.
Right?
And you should be able here it is.
Should be six instead of nine.
I just saw that mistake.
Now, what we've talked about before, you can use it here.
Don't worry about this.
These are just emoji here that I use, but read it carefully.
And if you can't solve it, just go back to the next to this one.
Actually, the easiest solutions and you will find the solution right now.
Take care and I will see you in the next one.