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.

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

 

相关文章
|
11月前
|
存储 JavaScript 前端开发
JS中return的用法
JS中return的用法
59 0
|
1月前
|
Web App开发 JavaScript 前端开发
JavaScript 严格模式(use strict)
JavaScript 严格模式(use strict)
35 0
|
2月前
|
JavaScript 前端开发
JavaScript 函数中break,continue,return 的区别
JavaScript 函数中break,continue,return 的区别
30 0
|
5月前
|
JavaScript 前端开发 安全
javascript中的严格模式(use strict)
javascript中的严格模式(use strict)
46 1
|
5月前
|
JavaScript 前端开发 开发者
js代码中“use strict” 是什么意思? 使用它的区别是什么?
js代码中“use strict” 是什么意思? 使用它的区别是什么?
67 1
|
5月前
|
JavaScript 前端开发 开发者
js代码中“use strict” 是什么意思? 使用它的区别是什么
js代码中“use strict” 是什么意思? 使用它的区别是什么
76 1
|
5月前
|
JavaScript 前端开发
JS中return的作用是什么
JS中return的作用是什么
47 0
|
5月前
|
JavaScript 前端开发 安全
js 代码中的 “use strict“; 是什么意思 ?
js 代码中的 “use strict“; 是什么意思 ?
|
10月前
|
数据库连接
(node:2612) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
(node:2612) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
|
JavaScript 前端开发 安全
js 代码中的 “use strict“; 是什么意思 ?
js 代码中的 “use strict“; 是什么意思 ?
94 1