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
An introduction to asynchronous programming in Express.
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
[MUSIC]
0:00
Hey, I'm Treasure, web developer and
teacher here at Treehouse.
0:09
Writing asynchronous code
in Express often involves
0:13
acting upon data whenever
you happen to receive it.
0:16
For example, say I want to retrieve
a list of movies from a database and
0:19
render that movie data into a template.
0:23
Because of the asynchronous nature of
working with the database, there's no
0:26
guarantee I'll get that movie information
back before Express renders the template.
0:30
The result, an empty HTML
template sent to the browser.
0:35
To keep that from happening,
you need a way to make sure you have
0:39
all the data you need before
the template is rendered.
0:42
Traditionally, Express uses call backs
to deal with asynchronous tasks.
0:46
You'd call a function that
retrieves a list of movies, and
0:50
that function would take
a call back that runs when and
0:53
only when the list of movies has returned,
or an error has occurred.
0:56
This is fine if the actions
you're performing are simple.
1:01
It's when you have several asynchronous
tasks to perform before you send something
1:04
to the browser that things can get messy.
1:08
For example, if you needed to retrieve a
certain subset of movies from a database,
1:10
then the ratings for
1:15
those movies, then a list of actors
who starred in those movies.
1:16
You'd end up with a call back, nested
in a call back, nested in a call back.
1:20
We'll see an example of
this in a later video.
1:24
This is one reason that approaches to
asynchronous programming in JavaScript
1:28
have evolved and
changed a lot over the years.
1:31
Making the async landscape sometimes
difficult and overwhelming to navigate.
1:34
Recently, new approaches have become
available natively in JavaScript.
1:39
In this course we'll look more
closely at two other approaches and
1:43
how you can use them in Express,
Promises and async/await.
1:46
These approaches are used by
popular libraries like Mongoose and
1:50
Sequelize that help you
interact with databases.
1:54
As a developer, you're likely to encounter
and will need to understand how these
1:57
three approaches, callbacks, Promises,
and async/await are used in Express.
2:01
This course assumes that you're
familiar with the basics
2:06
of building a server side
application with Express.
2:09
You should understand what
asynchronous programming is,
2:12
feel comfortable with callbacks, and
have some familiarity with Promises.
2:15
If you've never heard of
asynchronous programing or Promises,
2:19
see the teacher's notes for resources
that should help you get up to speed.
2:22
While asynchronous programming can be
a big and confounding topic in JavaScript,
2:25
I hope you'll finish this workshop feeling
more confident in your knowledge about how
2:30
each of these approaches are used
to build applications in Express.
2:34
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