ExpressJS next (error) vs return next (error)

What is the difference between next (error) and return next (error)

How to throw business exceptions in ExpressJS

+8
javascript express
source share
1 answer

Express return not required. next(error) is enough for him.

 function foo(req, res, next) { next(new Error()); } 

But return can also be used to stop the execution of the current function , allowing next(error) more closely resemble the throw statement.

 function foo(req, res, next) { return next(new Error()); console.log("This is unreachable code and won't be logged."); } 
+11
source share

All Articles