字幕表 動画を再生する
all right.
It's time.
Yes, eight.
I have never talked about Yes, eight before.
But this video, I'm going to look at something called a sink and a weight.
And it's part of S eight, which is, yes, 2017.
But anyway, this is really what's often referred to as syntax sugar.
So basically, we're not going to get any new functionality.
But we're going to have a different way to write in a synchronous function that returns a promise that just makes things easier to follow and nicer, I think.
I mean, you can choose to decide whether it's better or not, but I think I like it.
It's new for me.
I haven't really worked with this until yesterday, so let's see how this goes.
All right, So just to review, if you watch the previous video I wrote this function called Delay.
This function, called DeLay, returns anew.
Promise and up.
There's one mistake here, so this isn't that big of a deal.
Once the promises rejected, I really should stop but not do anything else.
But this code keeps going and calls this set time out.
It won't resolve the promise, but still doing this weird set time out, so I could There's some options here.
I could say, like, return.
Um, I could also I think what I'm just gonna do for my way I like to do things is just putting else here.
So I'm gonna add that to dysfunction.
Receives a number, creates a promise.
Oh, receives an argument if the receives a perimeter, if the parameter is not a number, it rejects the promise and throws it an error.
If it is, it calls set time out and resolve the promise.
That way I can say after a certain delay, like 1000 milliseconds create a paragraph or catch the air.
So now what I'm gonna do is I'm going to write DeLay s eight.
So here's the thing.
If a function returns a promise, if a function returns a promise and what returns a promise, the delay function returns a promise.
So I'm gonna do something weird here.
Um, so I'm gonna What I want to do is call DeLay e s eight.
I'm just I'm writing a new function.
I'm gonna call that this is going to stay exactly the same.
This is going to stay exactly the same Now, this is really weird.
And you know what?
The next video.
I'm gonna do this with the word Nick and Giffey example that's gonna make way more sense.
I probably just do that now, but I'm already going down there.
You can skip to the next video if you want.
This is a little bit weird.
You almost want to forget Pretend that delay isn't a function that I wrote.
But DeLay is a function that's part of some JavaScript library that I've imported and DeLay returns A promise.
So I'm trying to have a better name for this than DeLay s eight, but I guess I'll keep that right now.
Um, so what this allows me to do, right?
If a function this function, I've been taking a long time to get to this returns A promise.
If that's the case, I can suddenly use this key word await meaning.
Just wait for the promise to resolve.
It's almost like it's kind of like writing blocking code so I could say await delay time, await delay time.
Then I can return.
Now.
This is why I really want to do this with the fetch function, because there's so much more.
This is like a very tiny little bit.
But this now should it's gonna make much more sense when I actually have to do more steps, Do anything, just awaiting that.
I don't do anything after, but this now will automatically return problems.
I'm missing an important piece.
Let's just run this and see what happens.
I don't know if I like this video so far.
It might have toe rethink this, but I'm going.
I'm going.
I'm going with it.
Let me refresh.
Await is on Lee Valid in an AI think function.
Oh dear.
So here's the thing.
The await keyword kid, Just use it anywhere in your code like Oh, wait for this.
Then do this then wait for this.
You have to write your own a synchronous function.
Basically, you have to write a function that returns a promise.
But rather than having to say new return return new promise.
The A sink keyword just says, Hey, do all that stuff kind of invisibly behind the scenes for me.
So now if I come back over here and I tag this function, basically attacking is the wrong word, but I modify by giving this function of modifier a sink.
I say this is an a synchronous function.
It's going to execute a synchronously and and when and return a promise.
After all, after however many calls to await that I want.
So now let me just hit refresh here and this works again.
So this now is an asynchronous function.
So this and the reason why this is exciting is I can start to do this so I can sequence a bunch of things that are a synchronous that and and and some of these might actually return something.
And this now, instead of having to chain all these different promises with then dot Tenn dot then that then that Then catch this catch that I could just do it all in one function and that function will return a promise.
So I think I don't know how much I don't know if this was that useful to you, hopefully gives you kind of a sense, but I think a practical example will make a lot more sense.
So if you remember this particular example, look at what I had to do here I had to fetch from word Nick.
Then get the response converted to Jason.
Then convert that.
Then get the word out of that, then go to another A p I I'm going to rewrite all of this in an asynchronous function using a wait.
And I think that's going to help make things make more sense.
So that's what's gonna be in the next video.
Okay, See you there.
Maybe, maybe not.