简体   繁体   中英

ExpressJS next(error) vs return next(error)

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.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM