"Ruby Operators and Control Structures" was retired on November 27, 2017. You are now viewing the recommended replacement.
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 REST APIs with Express!
You have completed REST APIs with Express!
Using try/catch blocks to handler errors for each route can get messy after a while. We can write a middleware function that can abstract away the try/catch block to DRY up our code.
Snippet
function asyncHandler(cb){
return async (req, res, next)=>{
try {
await cb(req,res, next);
} catch(err){
next(err);
}
};
}
- If you'd like to know more about how this function, works, review the Reduce Error Handling Code When Using Async/Await video.
- npm module for handling async/await syntax 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
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