Express: next is not called properly inside a catch after await


I have a very simple route

router.get('/', async (req, res, next) => {
  try {
    const allEmployees = await employees.find({});
  } catch (error) {

It works ok. But after I refactored it with catch. it stopped working and threw UnhandledPromiseRejectionWarning:

router.get('/', async (req, res, next) => {
  const allEmployees = await employees.find({}).catch(next)

It seems like the next is not called correctly in the second version. But Both should be equivalent in JavaScript. not sure why the second one is broken in Express.


Both should be equivalent in JavaScript

No, they are not.

Promise.prototype.catch() returns a new promise that resolves with the return value of the callback. Since next() returns void, this code…


returns a successful promise, resolving with undefined. next() will be called with the failure but there’s nothing stopping the rest of your code from calling res.json(undefined).

If you want to use the Promise prototype methods, the equivalent would be

router.get('/', (req, res, next) => {

If you wanted to keep using async / await and have the eventual promise fail, you’d need something like this

router.get("/", async (req, res, next) => {
  const allEmployees = await employees
    .catch((err) => Promise.reject(next())); // rejected result

Answered By – Phil

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published