我正在尝试使用异步模块运行我的代码。但是,当async.series运行时,它只执行第三个()函数然后停止。我知道async.series的第一个参数需要一个需要回调的任务数组。但是,我不确定我应该如何在函数first()和second()中从另一个文件导出的函数上进行回调。有什么帮助吗?
var process = require('child_process');
function executeProcess() {
process.exec(...);
}
exports.Process = function() {
executeProcess();
}
var process = require('./process.js');
function first() {
process.Process();
}
function second() {
process.Process();
}
function third() {
console.log('third');
}
function parallel() {
async.parallel([first, second], function() {
console.log('first and second in parallel');
});
}
async.series([third, parallel], function() {
console.log('third then parallel');
});
答案 0 :(得分:0)
您应该使用回调函数。我认为你的process.js还可以,所以我没有改变它。但我在app.js做了一些小的改动。刚刚添加了回调函数:
var process = require('./process.js');
var async = require("async");
function first(callback) {
console.log('FIRST');
process.Process();
callback();
}
function second(callback) {
console.log('SECOND');
process.Process();
callback();
}
function third(callback) {
console.log('third');
callback();
}
function parallel() {
async.parallel([first, second], function(callback) {
console.log('first and second in parallel');
});
}
async.series([third, parallel], function() {
console.log('third then parallel');
});
这应该有效。这是一篇关于异步库的非常好的博文: