nest async.waterfall in async.forEachSeries
nest async.waterfall in async.forEachSeries
我构造了这个简化的小例子来说明我的异步问题:
var array = ["a", "b", "c", "d"];
async.forEachSeries(array, function(entry, callback){
console.log(entry);
async.waterfall([
function(cb){
console.log("step 1");
cb(null, "x");
},
function(param, cb){
setTimeout(function(){console.log(param, "step 2");}, 1000);
cb(null, "xx");
},
function(param, cb){
console.log(param, "step 3");
cb("xxx");
}
], function(result, err){
console.log(result, "waterfall done");
callback();
});
}, function(err){
if(err) console.log("ERROR: "+ err);
});
其输出为
a
step 1
step 3 xx
end xxx
b
step 1
step 3 xx
end xxx
c
step 1
step 3 xx
end xxx
d
step 1
step 3 xx
end xxx
step 2 x
step 2 x
step 2 x
step 2 x
如何确保瀑布中的函数等待前一个函数完成?
谢谢
我相信
您必须将cb(null, "xx");
放入步骤 #2 的setTimeout
回调中。
setTimeout(function() {
console.log(param, "step 2");
cb(null, "xx");
}, 1000);
相关文章:
- jQuery加载的async和ready函数不工作
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- 超过了async.detect最大调用堆栈大小
- 如果数组仍在执行,async.forEach()将运行我添加到数组中的新元素
- node.js应用程序中的async.js问题
- 使用async.parallel时出错
- async.js和系列问题
- async问题,JS Promise无法返回结果,但可以使用console.log
- 在Redux中链接async和sync操作以计算初始状态
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作
- 使用ajax调用中的数据而不使用async:false
- Node.js时使用async是有意义的
- async.parallel中的回调是如何工作的(它来自哪里?)
- Javascript Async thunk
- async.retry-已调用回调
- jQuery没有指定异步调用级别async:false
- node.js,async,restify:无法使restify调用在async.series内同步
- 如何防止bind在使用async.series时更改node.js中的原始对象
- Nodejs async.each 等待第一个循环
- nest async.waterfall in async.forEachSeries