Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressJS next(error) vs return next(error)

What's difference between next(error) and return next(error)

How to throws Business Exceptions in ExpressJS

like image 357
Ego Slayer Avatar asked Nov 17 '13 16:11

Ego Slayer


1 Answers

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.");
}
like image 91
Jonathan Lonowski Avatar answered Sep 25 '22 06:09

Jonathan Lonowski