I'm trying to get my code to run using the async module. However, when the async.series runs, it only executes the third() function then stops. I understand that the first argument of async.series takes an array of tasks that requires a callback. However, I'm unsure as to how I'm supposed to do a callback on a function that I've exported from another file as in the functions first() and second(). Any help?
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');
});
You should use callback functions. I think your process.js is ok so I have not changed it. But I made some minor changes in the app.js. Just added there callback functions:
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');
});
This should work. Here is an very good blogpost about async library:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.