如何跳过"async.forEachOf"在Node.js中循环迭代
How to Skip a "async.forEachOf" loop iteration in Node.js
async.waterfall
嵌套在async.forEachOfLimit
循环中,如下面的代码所示。
问题:当代码在async.waterfall
内执行步骤时,如何跳过async.forEachLimit
的迭代?换句话说,脱离async.waterfall
回到async.forEachLimit
。我在代码中注释了应该进行此检查的位置
当前代码给出错误Callback was already called.
同样,当我想打破async.waterfall
时,如果我使用return callback()
代替cb()
,则不会发生错误,但不会跳过。
var async = require('async')
var users = ['a','b','c']
async.forEachOfLimit(users, 1, function(user, index, cb) {
console.log(index + ': ' + user)
async.waterfall([
function(callback) {
callback(null);
},
function(callback) {
// Skip async.forEAchOfLimit iteration when index == 1
if(index == 1)
cb()
callback(null);
}
], function (err, result) {
console.log(index + ": done")
cb()
});
}, function() {
console.log('ALL done')
})
误差0: a
0: done
1: b
2: c
1: done
/Users/x/test/node_modules/async/lib/async.js:43
if (fn === null) throw new Error("Callback was already called.");
^
Error: Callback was already called.
期望输出值
0: a
0: done
1: b
2: c
2: done
ALL done
使用return callback()
var async = require('async')
var users = ['a','b','c']
async.forEachOfLimit(users, 1, function(user, index, cb) {
console.log(index + ': ' + user)
async.waterfall([
function(callback) {
callback(null);
},
function(callback) {
// Skip async.forEAchOfLimit iteration when index == 1
if(index == 1)
return callback()
callback(null);
}
], function (err, result) {
console.log(index + ": done")
cb()
});
}, function() {
console.log('ALL done')
})
0: a
0: done
1: b
1: done
2: c
2: done
ALL done
在您的第一个解决方案中,当索引匹配1时,cb
被调用两次,这就是为什么您不断获得Callback was already called
错误。虽然你调用forEachOfLimit回调cb
,你的代码不会停止执行和调用回调。在回调函数中,cb
再执行一次。
var async = require('async')
var users = ['a','b','c']
async.forEachOfLimit(users, 1, function(user, index, cb) {
console.log(index + ': ' + user)
async.waterfall([
function(callback) {
callback(null);
},
function(callback) {
// Skip async.forEAchOfLimit iteration when index == 1
if(index == 1)
cb() // First callback call
callback(null);
}
], function (err, result) {
console.log(index + ": done")
cb() // Second callback call
});
}, function() {
console.log('ALL done')
})
在第二种解决方案中,如果index匹配1,则不带参数调用callback,并跳过带空参数调用callback。还是没有冲出瀑布
用瀑布法解决你的问题,你有两个选择。
用一个error参数调用waterfall的callback方法,它会跳出waterfall,然后在waterfall的回调中处理这个错误。
var async = require('async') var users = ['a','b','c'] async.forEachOfLimit(users, 1, function(user, index, cb) { console.log(index + ': ' + user) async.waterfall([ function(callback) { callback(null); }, function(callback) { // Skip async.forEAchOfLimit iteration when index == 1 if(index == 1) return callback(new Error('Index equals 1')); callback(null); } ], function (err, result) { console.log(index + ": done") if(err.message == 'Index equals 1') { err = null; // If you want to continue executing forEachOfLimit no error must be passed to cb } cb(err, result); }); }, function() { console.log('ALL done') });
在每个瀑布式方法的开头跳过其余的代码并立即调用回调(这是你在第二次尝试中所做的)
var async = require('async') var users = ['a','b','c'] async.forEachOfLimit(users, 1, function(user, index, cb) { console.log(index + ': ' + user) async.waterfall([ function(callback) { callback(null); }, function(callback) { // Skip execution of the rest of waterfall method immediately if(index == 1) return callback() // Some additional code here callback(null); } ], function (err, result) { console.log(index + ": done") cb() }); }, function() { console.log('ALL done') })
相关文章:
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- "未捕获的语法错误:意外的标记"角度JS
- html,js-如何限制元素"范围“-命名空间
- "可以't查找变量:window“;在suds.js中
- Node.js服务器问题-已添加“/"在浏览器中查找文件时
- 如何创建js常量对象与"依赖/相对;价值观
- "TypeError:t.start未定义”;,在fullcalendar.min.js中,同时在我的网站上使
- "回声“;Node.js中的图像
- 显示“<script src='some.js'></脚本>"在Html文档中
- Django,HTML,JS:<img src=“#">可以在HTML中工作,但不能在JS文件中工作
- linkedin js"获取我的关系”;问题
- "现代的“;在Google Maps JS API v3上使用字母标记
- “*[attribute ^="string”是如何/为什么是有效的querySelector?(JS bug
- 在Asp.net的空网站中,如何改变"Web.config”;使内容(.js)文件显示在浏览器中,而不是下载
- 改变与“"至“-"(JS/Jquery)
- 重复“;function(){}.调用(this)"在Uglified JS中
- Sails.js+Grunt"找不到模块“”;错误
- 要求JS 2.1.9引起“;最大调用堆栈"使用Grunt时出错
- "捆绑;仅适用于HTML/JS的解决方案
- JS/JQuery:动态添加"图案“;以及“;标题“;属性以形成输入