all right it's time yes 8:00 I have
never talked about es8 before but this
video I'm going to look at something
called a sink and a weight and it's part
of es 8 which is es 2017 but anyway this
is really what's often referred to as
syntax sugar so basically we're not
gonna get any new functionality but
we're going to have a different way to
write in asynchronous function that
returns a promise that just makes things
easier to follow and nicer I think I
mean we can you can choose to decide
whether it's better or not but I 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 your review if you watched the
previous video I wrote this function
called delay this function called delay
returns a new promise and uh ooh I
there's one mistake here so this isn't
that big of a deal
once the promise is rejected I really
should stop and not do anything else but
this code keeps going and calls this set
timeout it won't resolve the promise but
still doing this weird set timeout so I
could there's some options here I could
say like return I could also I think
what I'm just gonna do for my the way I
like to do things is just put an else
here so I'm gonna add that so this
function receives a number creates a
promise o receives an argument if the
receives a parameter if the parameter is
not a number it rejects the promise and
throws an error if it is it calls set
timeout and resolve the promise that way
I can say after a certain delay like
1,000 milliseconds create a paragraph or
catch the error so now what I'm gonna do
is I'm gonna write delay es8 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 so
I'm gonna what I want to do is called
delay es 8 so I'm just I'm writing a new
function I'm gonna call that this is
gonna stay exactly the same this is
gonna stay exactly the same now this is
really weird and you know it in the next
video I'm gonna do this with the word
Nick and gif V example that's gonna make
way more sense I probably should just do
that now but I'm already going down this
or 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 and I'm
trying to think of a better name for
this and delay es8 but I guess I'll keep
that right now so what this allows me to
do right if a function this function I'm
taking a long time to get to this
returns a promise if that's the case I
can suddenly use this keyword a wait
meaning just wait for the promise to
resolve it's almost like it's kind of
like writing blocking code so I can say
a wait delay time a wait 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 going to make much more
sense when I actually have to do more
steps to do anything just awaiting that
I don't do anything after but this now
will automatically return prophets now
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 I
might have to rethink this but I'm going
I'm going I'm going with it let me
refresh a wait is only valid in an a
sync function oh dear so here's the
thing the oh wait keyword kit 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 asynchronous
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 think 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 a modify or a sync I say this
is an asynchronous function it's going
to execute asynchronously and and when
and return a promise after all after
however many calls to a weight 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 that's
the reason why this is exciting
is I can start to do this so I can
sequence a bunch of things that are
asynchronous 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 n dot then dot then dot 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 this was that
useful to you um 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 Wernick then get the response
converted to JSON then convert that then
get the word out of that then go to
another API I'm gonna rewrite all of
this in an asynchronous function using a
weight 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
[Music]