问题描述
与异步模块争夺了半天但是在嵌套几个级别时无法使其正常工作。
Been fighting with async module for half a day but can't get it to work properly when nesting few levels.
所以这样可行:
var async = require('async')
var myarr = ["Outer - A", "Outer - B"];
var myarr2 = ["Inner - A", "Inner - B"];
var innerComplete = true;
async.eachSeries(myarr, function( item, outerCallback) {
console.log('Processing item ' + item);
async.series([
function(callback) {
takeTime(2000, item, callback)
},
function(callback) {
takeTime(1000, item, callback)
},
function(callback) {
outerCallback();
}
], function(err) {
console.log("---OUTER SEQUENCE---")
})
}, function(err){
console.log("---OUTER LOOP---")
});
function takeTime(ms, msg, callback) {
console.log("Starting " + ms + " ms task from " + msg);
setTimeout( function() {
console.log("Finished " + ms + " ms task from " + msg);
callback();
}, ms);
}
它按顺序输出一切顺序如下:
And it outputs everything nicely in sequence like this:
Processing item Outer - A
Starting 2000 ms task from Outer - A
Finished 2000 ms task from Outer - A
Starting 1000 ms task from Outer - A
Finished 1000 ms task from Outer - A
Processing item Outer - B
Starting 2000 ms task from Outer - B
Finished 2000 ms task from Outer - B
Starting 1000 ms task from Outer - B
Finished 1000 ms task from Outer - B
---OUTER LOOP---
但当我尝试将另一个eachSeries循环嵌入其中时:
But when I try to nest another eachSeries loop into it like this:
var async = require('async')
var myarr = ["Outer - A", "Outer - B"];
var myarr2 = ["Inner - A", "Inner - B"];
var innerComplete = true;
async.eachSeries(myarr, function( item, outerCallback) {
console.log('Processing item ' + item);
async.series([
function(callback) {
takeTime(2000, item, callback)
},
function(callback) {
takeTime(1000, item, callback)
},
function(callback) {
async.eachSeries(myarr2, function( item2, outerCallback2) {
console.log('Processing item ' + item2);
async.series([
function(callback2) {
takeTime(2000, item2, callback2)
},
function(callback2) {
takeTime(1000, item2, callback2)
}
], function(err) {
console.log('---INNER SEQUENCE---')
})
}, function(err){
console.log("---INNER LOOP---")
});
outerCallback();
}
], function(err) {
console.log("---OUTER SEQUENCE---")
})
}, function(err){
console.log("---OUTER LOOP---")
});
function takeTime(ms, msg, callback) {
console.log("Starting " + ms + " ms task from " + msg);
setTimeout( function() {
console.log("Finished " + ms + " ms task from " + msg);
callback();
}, ms);
}
在输入第二个eachSeries循环时丢失执行顺序如下:
It loses the execution order when entering the second eachSeries loop like this:
Processing item Outer - A
Starting 2000 ms task from Outer - A
Finished 2000 ms task from Outer - A
Starting 1000 ms task from Outer - A
Finished 1000 ms task from Outer - A
Processing item Inner - A
Starting 2000 ms task from Inner - A
Processing item Outer - B
Starting 2000 ms task from Outer - B
Finished 2000 ms task from Inner - A
Starting 1000 ms task from Inner - A
Finished 2000 ms task from Outer - B
Starting 1000 ms task from Outer - B
Finished 1000 ms task from Inner - A
---INNER SEQUENCE---
Finished 1000 ms task from Outer - B
Processing item Inner - A
Starting 2000 ms task from Inner - A
---OUTER LOOP---
Finished 2000 ms task from Inner - A
Starting 1000 ms task from Inner - A
Finished 1000 ms task from Inner - A
---INNER SEQUENCE---
我也尝试过瀑布,mapSeries等,但是使用相同或其他混乱的执行顺序。我做错了什么或异步模块不支持这样的嵌套?
I also tried out waterfall, mapSeries etc, but with same or otherwise messed up sequence of execution. Am I doing something wrong or doesn't the async module support such nesting?
推荐答案
你没有调用 outerCallback2
,你没有调用回调
,而你正在调用 outerCallback
立即。
You're not calling the outerCallback2
, you're not calling the callback
, and you're calling the outerCallback
immediately.
修正:
async.eachSeries(myarr, function( item, outerCallback) {
,----------------------------------------'
| console.log('Processing item ' + item);
| async.series([
| function(callback) {
| `--------------,
| takeTime(2000, item, callback)
| },
| function(callback) {
| `--------------,
| takeTime(1000, item, callback)
| },
| function(callback) {
| ,-----------'
| | async.eachSeries(myarr2, function( item2, outerCallback2) {
| | ,---------------------------------------------'
| | | console.log('Processing item ' + item2);
| | | async.series([
| | | function(callback2) {
| | | takeTime(2000, item2, callback2)
| | | },
| | | function(callback2) {
| | | takeTime(1000, item2, callback2)
| | | }
| | | ], function(err) {
| | | console.log('---INNER SEQUENCE---')
| | `---> outerCallback2(err); // <<<
| | })
| | }, function(err){
| | console.log("---INNER LOOP---");
| `---> callback(err); // <<<
| });
| }
| ], function(err) {
| console.log("---OUTER SEQUENCE---")
`---> outerCallback(err); // <<<
})
}, function(err){
console.log("---OUTER LOOP---")
console.log("everything done");
});
这篇关于嵌套节点async.eachSeries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!