So I have a nice chunk of nested async code running together and it all seems to be okay, except for when I get to the end of it. my last function in a series block is a forEach: that then goes into a async.parallel
Managed to track down the following chunk which does not run in order:
async.forEach(array, function(elem, callback) {
async.parallel([
function(callback) {
database-call-A(elem, unction(err, data) {
if(err){
console.log("error on first parallel");
callback({err: false}); // will break out
} else {
elem.c = data;
callback();
}
});
},
function(callback) {
database-call-B(elem, function(err, data) {
if(err){
console.log("error on first parallel");
callback({err: false}); // will break out
} else {
elem.c = data;
callback();
}
});
}
]); // end async.parallel
// if forEach needs a callback after every iteration (which I think it does?)
console.log("PRINTS N TIMES - ONCE FOR EVERY ITERATION");
callback(); // both parallel functions have run, call back forEach
}); // end forEach
console.log("Donions - prints when finished");
When I tested this code by throwing print statements everywhere, I noticed that "PRINTS N TIMES ..." ran N times, then I got "Donions .." and THEN my do something(); and other stuff(); started getting called in my async.parallel.
Why is my forEach iterating through without waiting for the callbacks from async.parallel?