[MUSIC]
>> Hi there. I'm Christopher Harrison,
Senior Program Manager inside of Microsoft Cloud and AI.
Let's take a look at this relatively new,
at least, a JavaScript concept called Async/Await.
Now, what Async/Await is going to do for us;
is it's going to help make asynchronous code look synchronous.
That while Promises are cleaner,
they are not necessarily perfect.
That they can still add a little bit of bloat to our code,
and they require like a little bit of a different paradigm.
They wind up causing you to think about how it is
that you're going to write your code in
a relatively different fashion.
Because after all, you're still
using callbacks just with a different syntax.
This is where Async/Await comes into play.
Now, Async/Await is going to allow us to
make asynchronous code look a little bit more synchronous.
You're also going to notice that Async/Await is rather
common in a lot of other programming languages as well.
So let's take a look at our Async/Await syntax,
and then we're going to get in and take a look at our demo.
Now, you're going to notice up at
the top that I've got that promiseTimeout,
returning our new Promise,
and everything that we had from before.
So if you've already watched our section on Promises,
which if you haven't,
you should definitely go take a look at it.
Then what you'll notice is
the fact that we've already seen that syntax.
Now, what we're going to do here that's going to be
different is we're going to take advantage of Async/Await.
This will actually work with anything
that's going to return a Promise.
We start off our Async/Await by utilizing Async.
You use Async when you're
declaring a function to indicate the fact
that it's going to have an Await call inside of it.
Now, the rest of my function
can do whatever it is that it might want
and then eventually, here, we're going to have an Await.
Now, you'll notice that I put inter quotes here
that it pauses while the operation completes,
it pauses our code execution.
Now, what's actually happening here;
is it's going to go ahead, make the call,
wait for our Promise here, and then once it finishes,
it's going to evaluate, and then run the next line of code.
But keep in mind that other things could still happen.
That this is still our indication that,
"Hey, this is going to take a minute,"
so the thread could be then used for other operations.
So while it looks synchronous,
it's actually still going to be asynchronous.
You'll notice that I'm going to return back the number 42 here.
What's going to happen is this will actually be wrapped
inside of a Promise automatically for us.
So it's basically the same as us calling that promise.resolve,
that it will automatically be wrapped up.
Why is this important?
Well, it's important because now, I can use this function
like any other function that returns back a Promise.
So I can Await that function as well.
So I can then go ahead call
that simulated long operation and get the value back.
Now, because of the fact that it's going to give me a value,
what you'll notice is I'm going
to go ahead and say, "const" whatever my value is,
"= await", and then the function call.
So it will give me back, in this case, that number 42,
we'll be able to then print it out.
Now, the way that I then execute all of my code is by
calling that run command that you see down at the very bottom.
If there are any error messages,
I'm going to grab those by utilizing try/catch,
which by the way,
is another great advantage to utilizing Async/Await
because it allows me to now use
syntax that I'm already familiar with.
Let's see Async/Await in example form, in demo form.