A lot of section we spoke about refactoring from traditional promises over to using the improved async
await syntax.
Let's now apply all that knowledge to our callback function inside of the passport.
Fine.
So as a reminder I'm inside of services passports Geass and I'm looking at the second argument to the
Google strategy.
Now at present we have our aero function right here and we've got two different promises and here one
is being used to make a query to the database to find an existing user with a given Google ID and then
the second is being used to create and save a new user to the database.
So remember any time we touch our database that is an asynchronous interaction it always returns a promise
and we have to deal with the promise appropriately.
So I want to say that we are going to really factor this function to use the async syntax rather than
the promises that it is using right now.
Now as we do this refactor we're going to do it somewhat quickly and I want you to really triple check
your code.
Make sure you've got the same code as I make sure that you don't have any typos because of course any
typos is going to lead to an error when you try to test this code out.
So let's do the refactor.
Let's follow the few steps that we have to go through every time we do it.
First off we're going to identify the function that contains some amount of asynchronous code.
And so I'm looking at the arrow function.
And looking at the arrow function right here.
This is the function that contains some asynchronous code.
So I'm going to mark the function as a B as being a synchronous nature.
So right before the argument list a place the async keyword next will identify all the different promises
that are being created inside the function.
I see one promise being created right here.
So when we do the find one request I see a second promise being created right here when we call save
on the newly created user.
So we're going to start off by putting the a weight keyword in front of each of these requests.
Or each of these promises.
So I will say a weight user not find one and await new user save.
Next we're going to do a little bit of new lining and indentation to remove all the then statements
that we have going to find the first dot then appear.
I'm going to put a new line right before it.
Notice how when the promise is resolved we assign the return value to a variable called existing user.
So rather than receiving existing user as a new argument or an argument to this function we will assign
the result of a weight user defined one to a variable with an identical name.
So I will say Konst existing user equals a wait.
Wa wa wa like so we will then clean up the then statement.
I will clean up the closing braces down here that we're matching up to that then statement.
So make sure you find the curly brace parentheses like so clean them up and then we will indent the
remaining logic like so.
So now everything is on the same line.
Now at this point we should be able to save this file.
And if you are using prettier it should not result in any type of error.
So if I save the file I just saved it and I didn't get any air from prettier.
So if you're using prettier.
Good time to save the file or run prettier and just make sure that you don't have any syntax errors
inside of here.
So now we're going to do our refactor for the second promise inside of here.
So here's awaits new user.
I'm going to put the dots save on the same line as the new user creation and other all in the same line
and then going to find the then statement I'm going to say rather than allowing the new user to be or
the newly created user to be assigned to this variable user inside the callback function I will assign
it outside of it.
So I'll say Konst user equals a new user save.
And then we are going to delete the entire dot then line and make sure that we bring over the dun call
as well.
So I'll say delete that thing and the we'll still call done with no end user like so.
OK so I'm now going to save this file again.
Again I should not have any errors or any syntax errors at this point in time.
So everything in here is looking pretty good.
I should be able to flip back over to my terminal and if I review my running server I should still see
like everything is running.
Yes I know we've got the deprecation warnings but I should not see any messages saying oh hey you have
some air in here.
Now at this point if you do see an error and you see something like oh unknown keyword async or something
like that or unknown token I want you to triple check and make sure that you are running an up to date
version of No.
Yes.
So if you open up a new tab or a new console window and run node dash Vi if you see anything earlier
than version like 7.6 or so you do not have support for a single weight you are running an old version
of Node.
And I remember the first thing that we didn't sign this course we updated our version of node and make
sure that made sure that we were running at least version 8 or so.
So if you're seeing an error over here that says something like Hey I don't know what the key word async
means.
That means you probably have an old out version or out of date version of node and you need to make
sure that you update it because you will not get support for async wait until you update your version.
So if you need to update note if you're seeing that error message go back to the very first lecture
in this course.
Make sure you update your version of note and you have at least a point 1.1.
OK so now back over at our code we can look at our function now and visually it looks a lot simpler
and a lot more straightforward than what we had before we can even do a little bit more of a refactor
than we have right now.
I'm gonna say that three factor I'm about to do is 100 percent optional but it's how I would personally
write this code out as this give me an optional Nery factor.
This is going to be what I would personally do inside of here.
You'll notice that we have if existing user.
And then the Elle's case right here we don't really need the L's case here.
If we make sure that we return when we hit this block.
So if I add the return keyword in front of done right here that will make sure that if we have an existing
user we will not run anything else inside the function.
And so then I can remove the Ell's case of the if statement.
Of course we still want the logic in there we just don't need the actual explicit else.
And so when we remove that thanks to the done or return key word right here everything inside of here
now gets even more straightforward than before.
And if I want to really clean things up I would even remove the comments and now wow this is remarkably
more Surt straightforward and remarkably more simple than what we had before.
So that's pretty much it.
That is the async await syntax.
Again we're going to use it throughout the rest of this course one or two times we might write out some
code using promises but then we'll refactor it immediately over to use the async await stuff instead.
So I hope that is a little bit more clear what's going on here.
Let's now continue inside the next section and continue developing the front end of our application.