码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-04-20 21:30:04      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   bsp   ret   when   second   ops   log   level   class   

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

标签:style   bsp   ret   when   second   ops   log   level   class   

原文地址:http://www.cnblogs.com/softidea/p/6740441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!