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 callbacks to handle asynchronous tasks in Express.
Example of a callback with performing two asynchronous operations:
app.get('/:id', (req, res) => {
getUser(req.params.id, (err, user)=>{
if(err){
res.render('error', {error: err});
} else {
getFollowers(user, (err, followers) =>{
if(err){
res.render('error', {error: err});
} else {
res.render('profile', {title: "Profile Page", user: user, followers: followers});
}
});
}
});
});
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
To follow along with me, download the
project files from the link on this page
0:00
and open them in your
preferred text editor.
0:02
We'll be using a very simple Express
application that retrieves information
0:04
about users from a JSON file and
renders it into a main index template.
0:08
Or if there's an error,
and error template.
0:14
Here's what that looks like in
the browser, and when there's an error.
0:17
Open up the app.js file.
0:22
Inside, you'll see all the trappings
of a basic Express application,
0:24
a function called getUsers,
and an empty Express route.
0:28
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