在我正在编写的测试中,使用以下内部回调再次陷入异步地狱。我已经评论了不等待的回调。我同时使用 async.series 来编组函数,并使用 async.each 来保持内部迭代同步。Mocha compalins “done() 被多次调用” - 为什么代码不等待?
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");
});
});