Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!