How do you catch async errors in Express 4?
Wrap with asyncHandler: Promise.resolve(fn(req, res, next)).catch(next). This catches promise rejections and passes them to the error handler. Express 5 catches async errors automatically.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Express Middleware Interview Questions and Answers
A function (req, res, next) that runs on requests. It can modify req, send a response, call next to continue, or call next(err) to jump to the error handler. Almost everything in Express is middleware.
With a four-parameter middleware (err, req, res, next) registered last. In handlers, throw or call next(err). Express skips to the error handler. Use a custom ApiError class with status and message.
Express runs middlewares in the order you add them. Body parser must come before routes (else req.body is undefined). Helmet should come early. Error handler must be last. Wrong order causes silent bugs.
Still have questions?
Browse all our FAQs or reach out to our support team
