Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We can use JavaScript's async/await keywords to handle asynchronous actions in our code, to make sure we have our quotes data before we respond to the client.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We're calling this method,
records.getQuotes, and
0:00
saving the result to
a variable called quotes.
0:03
records.getQuotes reaches into our data
store, gets all of the quotes, and
0:06
returns them to us.
0:10
Once we have all those quotes,
we send them back to the client as JSON.
0:11
At the moment, however,
we're getting back an empty object.
0:15
And this is because the getQuotes
function is asynchronous.
0:18
All the functions in the records.js
module use asynchronous functions to
0:22
closely mimic the behavior
of an actual database.
0:27
And accessing a database is
an inherently asynchronous operation,
0:30
meaning we can't be exactly sure when
we're going to receive the information.
0:34
And in the meantime, JavaScript just
keeps on going without waiting for
0:38
the data to return.
0:42
If you're new to the topic of asynchronous
JavaScript, aren't familiar with promises,
0:44
or are totally new to async and
await syntax.
0:48
I strongly recommend checking out
the resources linked in the teacher's
0:51
notes below.
0:54
The getQuote function returns a promise,
which uses Node's readFile method to
0:55
retrieve the data from the data.json file,
then resolves the promise with that data.
1:01
The problem here is that the res.json
method ends up running before
1:07
the getQuotes function has
a chance to return any data.
1:12
JavaScript gets to this line of code and
says, well, I guess there's
1:16
nothing in this quotes variable, and
moves onto the next line of code.
1:19
The promise never has a chance to resolve,
1:23
and as a result, we end up sending
back an empty object to the client.
1:26
What we need is a way to tell JavaScript,
hey, stop.
1:30
Just hold on and wait for
1:33
this information to come back before
running the next line of code.
1:34
We can do that using async and await.
1:38
We can await any function that returns a
promise, so we'll await records.getQuotes.
1:40
This tells JavaScript to expect
this method to return something, so
1:47
it shouldn't just give up on it and
move on.
1:51
It should wait and tell this method,
1:54
return something to execute
the next line of code.
1:56
The await function must be used
inside of an async function.
1:59
This basically lets JavaScript know, hey,
inside this function, we're going to ask
2:05
you to await some information
before you speed on down the line.
2:10
So let's save and test this out
by going back to the browser.
2:13
Refresh, and
now we're returning our list of quotes.
2:17
There is some error handling
that we could still do here, but
2:21
we'll get to that in a later video.
2:24
We'll continue to use async and await
to build out the rest of our project.
2:26
So rest assured that we'll see and
2:30
talk about this again if you're
still feeling unsure about it.
2:32
In the next video, we'll refactor our
other get route to use our data store and
2:35
async await.
2:39
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up