What's difference between next(error) and return next(error)
How to throws Business Exceptions in ExpressJS
The return
isn't needed by Express. next(error)
is sufficient for it.
function foo(req, res, next) {
next(new Error());
}
But, the return
can be used to also stop the execution of the current function
, allowing next(error)
to more closely resemble a throw
statement.
function foo(req, res, next) {
return next(new Error());
console.log("This is unreachable code and won't be logged.");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With