JavaScript Error Handling

Begana Choi
Begana Choi
Full Stack JavaScript Techdegree Student 8,884 Points

why next(err); not close the code the end and move forward?

in the video, Joel said that

  app.use( ( req,res,next ) => {
  const num = parseFloat(req.body.number);
  if( isNaN(num)){
     const err = new Error('Submitted value is not a number');
     ** here is where I'm talking about 
     next(err);
  }
  const sum = num * 2;
  req.doubled = sum;
  next();
});

it doesn't close out the middleware function. but why? can somebody explain to me why it occurs another error?

1 Answer

Steven Parker
Steven Parker
194,962 Points

The only way to end a function is with a "return" statement (or a "throw"). The instructor adds "return" to fix the issue just a few moments later in the same video.