Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  77] [ 7]  / answers: 1 / hits: 16994  / 5 Years ago, wed, july 10, 2019, 12:00:00

Hey I am trying to comprehend what does express-async-handler do?



It was used in one of the repo I was looking.



From their docs, it says




Simple middleware for handling exceptions inside of async express
routes and passing them to your express error handlers.




Link for the repo: https://www.npmjs.com/package/express-async-handler



Can someone please explain this with example?


More From » node.js

 Answers
51

The example in the npm page shows this:



express.get('/', asyncHandler(async (req, res, next) => {
const bar = await foo.findAll();
res.send(bar)
}))


Without asyncHandler you'd need:



express.get('/',(req, res, next) => {
foo.findAll()
.then ( bar => {
res.send(bar)
} )
.catch(next); // error passed on to the error handling route
})


The first one uses the async / await language elements and is more concise.


[#51898] Thursday, July 4, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarkulisesa

Total Points: 422
Total Questions: 93
Total Answers: 112

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
clarkulisesa questions
Mon, Feb 24, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;