我有这个简单的代码似乎与async.waterfall的使用方式一致,但我提到了这个TypeError:
var myCallback = function (data){
console.log("task done for " + data);
}
var test = function (param,callback) {
console.log("in test, doing some work, param is: " + param);
callback(param);
};
async.waterfall([
test("call 1",myCallback),
test("call 2",myCallback),
test("call 3",myCallback)
], function (err, result) {
// result now equals 'done'
});
执行结果是:
in test, doing some work, param is: call 1
task done for call 1
in test, doing some work, param is: call 2
task done for call 2
in test, doing some work, param is: call 3
task done for call 3
/Users/.../Documents/.../node_modules/async/dist/async.js:5021
task.apply(null, args);
^
TypeError: Cannot read property 'apply' of undefined
at nextTask (/.../node_modules/async/dist/async.js:5021:17)
at Object.waterfall (/.../node_modules/async/dist/async.js:5024:9)
at Object.<anonymous> (/.../app.js:77:7)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
我一直在四处寻找,但找不到问题 - 欢迎一些帮助。非常感谢! ç
答案 0 :(得分:1)
尝试以下代码:
async.waterfall([
function(callback) {
task1().then(function(){
callback(data)
})
},
function(data1, callback) {
task2().then(function(data2){
callback(data1, data2)
})
}
], function(err, data1, data2) {
console.log('data');
});
您的代码无效,因为无法调用最终回调并尝试调用另一个不存在的任务。