In this video now that we have a sense of familiarity with the async await syntax it's time to put it
into practice.
We're going to do a little work together.
Then there'll be a challenge then a little more work and then a second challenge trying to give us some
real hands on experience.
So let's talk about where we're going to start.
We're going to close all of our open files collapse the play ground directory and move back into Task
Manager.
Together we're going to convert promise chaining dot J asked to use async await.
Then you'll do a challenge well you'll convert promise chaining to then we're actually going to move
into index dot J S and use async await inside of the task manager API root handlers which I'm really
excited to get to because it's gonna allow us to clean up the code pretty significantly.
So to get things started.
What are we going to do.
Well right here we'll go ahead and kick things off by creating a new function that's going to be the
async function.
Remember to use a single wait we have to start with an async function.
So right here I can call this something like update age and count describing what it's doing I will
then set it up as an async function.
And right here I'll use an arrow function.
Now using async isn't limited to Arrow functions I could use it with standard functions or with the
E6 method definition syntax if I was defining a method on an object.
Right here though let's go ahead and actually get done what we need to get done now for this one we
do need to accept some input that is going to be the I.D. of the user we're updating and we're also
going to accept the age they want to change to down below we'll use those two pieces of input to go
through the process now we're working with two methods that return promises find by I.D. and update
and count documents and we'll be using a wait with both.
The first thing we're going to do is call find by I.D. and update.
So right here I'll create const user and I'll get its value from awaiting the following promise.
User dot find by I.D. and update and we'll pass that I.D. in right here that is the parameter from up
above.
So we're just going to pass it through to the function.
Now we also want to provide the updates we're trying to apply.
So we'll have our object right here where we set age equal to the new age like zero.
In this case we have a variable called Age from up above which we're gonna use.
And since we're setting the property to a value that comes from a variable of the same name we can just
use that shorthand we explored earlier in the class.
So right here we have our first call all done.
And now we can move on to the second one where we get the count.
I am going to await the following user dot count documents and here we are looking for documents where
age equals the value passed in for age.
So once again taking advantage of that.
Yes 6 syntax like we did up above.
Now we have access to both the user end of the account and we can choose to return whatever we want
to.
I could return an object adding both of these on as properties if I wanted to access both of them or
I could return an individual one such as returning count right here.
So at this point we can now go ahead and use our function.
And that's exactly what I'm going to do down below.
Update age and count I will provide those two arguments.
I'll copy the I.D. from up above pasting that in and then I'll pick a new age.
I'll bring that person from 1 to 2.
So right here we now have the exact same functionality set up using async await.
We can also go ahead and add on then and catch to handle any potential errors or to do something with
the result.
Right here I'm going to get access to the count now.
This could be called anything I want but I'm calling it count because I know it's going to be the count
that's what gets returned from the async function right here console log printing count then I could
set up catch down below.
Doing the same thing getting our error and dumping it to the console console log e.
Perfect.
So now we have everything in place and we can go ahead and test our work.
What I'm going to do is comment out the chaining example up above though I'll leave it in place and
we are going to run the file testing that our user gets updated over inside of robo three T.
I have my list of users right here in that first tab and if I refresh things I have a couple of users
with the age of 1.
Let's go ahead and run the script and see what we get.
C D dot dot forward slash Task Manager and from here we're going to run the file that is node playground
forward slash promise chaining dot J.
S when I run the script we should see the output showing up.
Right here I get my count one which is fantastic.
Now that we have this in place we can go ahead and make sure that things did indeed update in the database.
I'm going to move over to robo three ti refresh and right here the age is now 2.
And the reason I'm seeing one is because only one user has that age.
With this in place we have now used a sink and a wait to do something with Mongoose and this is the
first of four things I wanted to do in the video.
The second as mentioned is a challenge where you're going to use async await with the other example
where we deleted a task by its I.D. and then figured out how many tasks we still needed to complete.
Let's go ahead and talk about what I'd like you to do.
Right here there are a few steps for getting this one done.
So the goal is to use async and no wait.
You're going to create a new function and async function called delete task and count that's going to
perform the two operations we have up above.
You're going to accept the idea of the task to remove as the only parameter for the function and then
the function body you're going to use a weight to delete the task and to count up the incomplete tasks.
Now your new async function should return the count to the caller.
And that brings us to the next step which is to call the function attaching then and catch like we did
in the other example Last up test your work find a task to delete and make sure it actually gets removed
correctly.
Take some time to knock that out test your work and check the count for correctness when you're done
come back and click play
how'd that go.
Let's go ahead and kick things off together.
What I'm going to do up above is comment out the previous chaining example and focus on the new one
by defining our function right here.
So const delete task and count.
Now we want to set this up as an async function so I can actually use a weight inside of it.
I could use any type of function I wanted to hear I'll just use an arrow function inside of here.
We want to make sure that we are accepting the I.D. of the task to delete.
And the next thing to do is to perform those two operations we had up above.
First is delete the task.
Second is count up the incomplete tasks right here const task equals.
We'll use a weight with the following promise.
It's the one that comes back from calling task dot find by I.D. and delete.
And we're going to provide the I.D. which we have as the first parameter.
So that is the first async operation.
Now after that's done we're going to perform the second which is to get the count the incomplete task
count right here once again using a wait with task dot count documents because count documents returns
a promise.
If these methods didn't return a promise we wouldn't be able to use them with a wait.
Now what do we want to provide to account documents.
Just our filtering criteria.
I only want to count documents where completed is equal to False.
I don't want to count those completed tasks.
Next up on the list we want to make sure to return something meaningful and right here.
I'll just return the count.
Now it's time to actually put this to use down below.
I'm going to do just that.
Now I do need the idea of a task to remove.
So over in robo 3 T.
I'll head back over to the tasks tab.
If I refresh things I have just to I'm going to go ahead and delete the second one I'll edit the document
just to grab its I.D. and I'll bring that I.D. over to visual studio code.
From here we can now call the function delete task and count.
I'm going to provide that I.D. as the first and only argument and then I can set up my then call where
I get access to that count and I can also console dialog it
and finally we'll go ahead and setup a catch call where we get access to the error and right here we'll
log that as well.
Perfect.
With this in place we can remove the challenge comments and actually test things out to make sure that
the new code works exactly as the old example did.
Now you'll notice.
Well we do indeed get a value back from find my I.D. and delete.
We're not actually using it.
If we wanted to I could have just the following.
This is also perfectly valid and would function the same way.
The only advantage of actually storing the result in a variable is that I could use it if I needed to.
In this case though I happen to not need the task I got deleted.
I'm going to save the program and run things from the terminal right here.
That would be node playground forward slash promise chaining that's number two.
Dot J S so I'm gonna take a quick moment to run this.
I can see my count of 1 printing and if I refresh things over in robo 3 T I can see my 1 task was deleted.
And the reason I'm getting one is because I have just one left.
So there we go.
We've used async await together and you've now used it on your own.
Now this example does indeed use those Mongoose methods but it's not exactly part of our task manager
application.
So what we're going to do is shut things down from the terminal.
Clear the terminal output and we're going to close down both of these files.
We used them to get comfortable with the syntax but what I really want to do is explore how this is
gonna help us build out the task manager rest api and create code that's easy to write and maintain.
So in index dot J J.S. we already have a bunch of different routes.
These are our various predefined operations and all of them are doing something a synchronous and you
can see that in every single one of them.
We have calls to then and catch to do something with the promise whether it's fulfilled or rejected
but we can actually do is make all of our root handlers these functions a sync and then we can use async
await inside of them to create code that looks a bit more synchronous.
I wanted to cover that in this lesson but to keep these lessons at a reasonable length I'm going to
push that off to the next one.
So if you do have time go ahead and jump right in.
Otherwise watch that lesson the next time you get a chance.