【发布时间】:2018-07-05 10:21:41
【问题描述】:
我正在努力实现以下目标:
请考虑在尚不支持 async/await 的 jasmine 测试框架中运行它
async.waterfall 调用具有 async.each 的函数来触发模式和表的创建。异步瀑布中的步骤必须按顺序执行,即必须在创建表之前创建模式。我面临的问题是执行创建模式的第一个调用,但回调从未返回到 async.waterfall。因此, async.waterfall 中的下一步永远不会执行。
时间线或流程:
driverFunction (async.waterfall) 调用 createFunction。
createFunction(asyncCreateSchema 等)函数为数组中的每个文件调用 doSomething。
doSomething 执行一个 jar 文件并返回成功或错误。
这是我的代码:
'use strict'
let async = require('async');
function doSomething(file, done) {
console.log(file);
return done(null, true);
}
function asyncCreateSchema(files, done) {
async.each(
files,
function(file, callback) {
if (file.startsWith('schema')) {
doSomething(file, callback);
}
else{
callback();
}
},
function(err) {
if (err) {
console.log(err);
}
console.log('create schema done');
});
}
function asyncCreateTables(files, done) {
async.each(
files,
function(file, callback) {
if (file.startsWith('table')) {
doSomething(file, callback);
}
else{
callback();
}
},
function(err) {
if (err) {
console.log(err);
}
console.log('create schema done');
});
}
var files = ['schema.json', 'schema_1.json', 'table.json'];
async.waterfall([
next => asyncCreateSchema(files, next),
(nil, next) => asyncCreateTables(files, next),
],
function(err, res) {
if (err) {
throw new Error("Setup error: " + err.message);
} else {
console.log(res);
}
}
);
我在这里做错了什么?请使用 async npm 包说明本场景中回调函数的流程。
【问题讨论】:
-
在
else的情况下(不存在)callback永远不会被调用 -
我也试过了:
function(err){ if(err) { console.log(err); } done(null, true); }它永远不会到达这条线。 -
不在结果回调中,在迭代回调中!
if (file.startsWith('schema')) { doSomething(file, callback); } else /* hang! */; -
@Bergi 牛眼。但是控制权还没有返回到瀑布!
标签: javascript node.js asynchronous callback jasmine-node