Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
How do you properly stop execution of an express.js endpoint?

I have a middleware error handler that is working great but next(err) and return and return next(err) seems to not stop execution when dealing with promises. What is the proper way to stop execution of my code when an error is found?

For reference: err is a standard Error class in this case.

I don't think you need the code in userProvider.fetchFriends to help with this but if that's wrong please let me know.

  const uid = req.query.steamUserId;

  //Use the userProvider to get steam friend data
  const friendData = await userProvider.fetchFriends(uid)
  .catch(err => {
    return next(err); //Does not stop execution. Why? just next(err) doesn't either.
  });

  //Putting this after every catch works but seems very stupid. How do I avoid this?
  if(res.writableEnded) 
    return;

  ...Other code that runs but causes errors
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You've got two problems here.

First: next() is explicitly continuing to the next middleware or endpoint.

To finish dealing with the request, send a response.

const middleware = (req, res, next) => {
    if (something(req)) {
        next();
    } else {
        res.status(500).json({ ok: false, msg: "some error message" });
    }
}

Second: You need to watch your asynchronous logic carefully.

You can't:

  1. trigger something asynchronous
  2. send a response
  3. send a different response when the asynchronous function is complete

You can only send one response to a request.

Either:

  • Don't call next or res.something in the catch block and just log the error internally or
  • Don't call next or res.something outside the promise handling and move it to a then handler instead (you might want to switch to using async/await to make your logic easier to follow)
about 4 years ago · Juan Pablo Isaza Denunciar

0

The issue was that I was mixing async/await with .then/.catch. Needed to use try/catch.

ty @jonsharpe

export const getSteamFriends = async (req, res, next) => {
  try{
    const uid = req.query.steamUserId;

    //Use the userProvider to get steam friend data
    const friendData = await userProvider.fetchFriends(uid);
  
    //more code in the middle
  } catch(e) {
    return next(e);
  }
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda