Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Asynchronous Code in Express!
You have completed Asynchronous Code in Express!
Preview
An example of using async/await syntax to handle asynchronous tasks in Express.
What two asynchronous operations might look like using async/await:
app.get('/:id', async (req, res) => {
try {
const user = await getUser(req.params.id);
const followers = await getFollowers(user);
res.render('profile', {title: "Profile Page", user: user, followers: followers});
} catch(err){
res.render('error', {error: err});
}
});
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
Using async and
0:00
await, we can write express routes that
are even more straight forward to follow.
0:00
Another benefit of async await
syntax is that it's easier to debug.
0:04
When an error occurs in
a massive chain of events,
0:08
it can be difficult to pinpoint
exactly where the error occurred.
0:12
With Async away,
0:16
it's much easier to tell on which line
an error was thrown, because the syntax
0:17
instructor of your code is a lot like
using standard synchronous functions.
0:22
Let's see what this looks like.
0:26
Copy and paste the get route,
and delete what's inside.
0:27
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