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 promises to handle asynchronous tasks in Express.
An example of what using promises to perform two asynchronous operations might look like:
app.get('/:id', (req, res) => {
getUser(req.params.id)
.then((user)=>{
return getFollowers(user);
})
.then((user, followers)=>{
res.render('profile', {title: "Profile Page", users: 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
Promises do the same thing for us in a way
that tends to be more straight forward and
0:00
easy to read.
0:04
Promises allow us to clearly outline
the order in which things need to be done,
0:05
by chaining functions together in
the order we wish them to execute,
0:09
rather than nesting them
one inside of another.
0:13
Remember, we're dealing
with eventualities here.
0:17
Writing code to deal with info from a data
store whenever we happen to receive it.
0:20
When this data becomes available, a
promise promises to do something with it.
0:24
Let's see what that looks like.
0:29
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