Running into async hell again with the following inner callback in a test I am writing. I've commented the callback that doesn't wait. I'm using both an async.series to marshall the functions, and async.each to keep the inner iteration synchronous. Mocha compalins "done() was called multiple times" - why isn't the code waiting?
describe('Report Generation (R subsystem)', function () {
before(function (done) {
//clear test files
async.series([function (callback) { //1st
console.log('Delete local test files');
_.each(output_file_template, function (test_file) {
if (fs.existsSync(__dirname + '/../reports/' + test_file + user_file_code + '.png')) {
fs.unlinkSync(__dirname + '/../reports/' + test_file + user_file_code + '.png');
};
}); //..._.each
callback();
}, function (callback) { //2nd
console.log('Delete remote test files');
async.each(output_file_template, function (test_file, cb) {
console.log(test_file);
s3.del('reports/' + test_file + user_file_code + '.png', function (err, res) {
console.log("Delete err", err);
console.log("Delete result", res);
cb();
}, function(err) {callback(err);}); //s3.head
}); //...async.each
}], function (err, res) { //3rd
done(); //this should tell the begin() clause to complete
}); //...async.series
}); //...before
it('should not start this test until before() has finished!', function (done) {
console.log("1st test here");
});
});
, What I can see is, you are doing async.series with an array of 3 functions, but no controlling function at the end.
I assume, your code in it('should not start... is the one which should be in there.
So (I think) your code should look like this:
describe('My Test 1', function () {
//clear test files
async.series([
function (callback) { //1st
console.log('Delete local test files');
...
callback();
},
function (callback) { //2nd
console.log('Delete remote test files');
async.each(
output_file_template,
function (test_file, cb) {
console.log(test_file);
s3.del('reports/' + test_file + user_file_code + '.png', function (err, res) { //I can't get the following nested callback to wait
console.log("Delete err", err);
console.log("Delete result", res);
cb();
}); //...s3.head
},
function( err ) { // this is the control function for async.each, so now it waits after all s3 have finished
callback( err );
}
); //...s3.head
},
function (callback) { //3rd -> will be called now after the async.each (I don't think, you use it so can be deleted anyway)
callback();
}
],
function( err, result ) {
done(); // whatever this has to do with the last "it" -> here is the point, where the "before" is completely done
}
});
I didn't test the source, so maybe there are typos inside, but I think it shows the picture.