In node.js, I want to have one foreach async.parallel inside another. However I got "callback was already called" error (look at the comment on my code). I want the callback to the outer only called after the inner finished. How can I do that?
async = require("async");
var asyncTasks = [];
var items = [];
/* push elements to items */
items.forEach(function(item){
var asyncTasks2 = [];
var items2 = [];
asyncTasks.push(function(callback2){
item.someAsyncCall(function(){
/* push elements to items2 */
items2.forEach(function(item2){
item2.someAsyncCall(function(){
callback2();
})
}
asyncTasks2.push(function(callback2){
setTimeout(function(){
callback2();
}, 100000);
});
async.parallel(asyncTasks2, function(){
// All tasks are done now
doSomethingOnceAllAreDone();
callback(); // callback was already called
});
});
});
});
asyncTasks.push(function(callback){
setTimeout(function(){
callback();
}, 100000);
});
async.parallel(asyncTasks, function(){
// All tasks are done now
doSomethingOnceAllAreDone();
});