When to use next() and return next() in Node.js

Posted 沧海一滴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了When to use next() and return next() in Node.js相关的知识,希望对你有一定的参考价值。

Some people always write return next() is to ensure that the execution stops after triggering the callback.

If you don‘t do it, you risk triggering the callback a second time later, which usually has devastating results. Your code is fine as it is, but I would rewrite it as:

app.get(‘/users/:id?‘, function(req, res, next){
    var id = req.params.id;

    if(!id)
        return next();

    // do something
});

It saves me an indentation level, and when I read the code again later, I‘m sure there is no way next is called twice.

http://stackoverflow.com/questions/16810449/when-to-use-next-and-return-next-in-node-js

 

以上是关于When to use next() and return next() in Node.js的主要内容,如果未能解决你的问题,请参考以下文章

When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions

When to close cursors using MySQLdb

When to use HTML Helper?

(转)Three challenges you’re going to face when building a chatbot

Case When: Using > Dates and < Dates with multiple conditions

How to keep Environment Variables when Using SUDO