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
Using try/catch to handle errors when using async/await syntax can get repetitive and verbose. In this video we'll look at an example of how we can use a function to abstract away try/catch.
This video doesn't have any notes.
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
each time would make our code pretty
long and unwieldy after a while.
0:00
To get around this,
we can use a piece of middleware to wrap
0:00
each of our routes automatically
in a try-catch block.
0:03
That way we won't have to explicitly
write it over and over again.
0:06
Let's write that middleware
at the top of our file.
0:10
We'll name it asyncHandler and
it will take a callback.
0:13
Inside the asyncHandler function we'll
return an asynchronous function that will
0:23
serve as our route handler's callback.
0:27
So it'll take the req,
res and next parameters.
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