我想编写一个用多个同步调用调用自身的函数(这里的代码只是流程的一个剥离示例)。问题是nodeJS给了我一个“TypeError:asyncCb不是一个函数”。
我研究了这个错误,似乎参数返回的不是函数,但我找不到代码中的错误。我是一个新的nodeJS开发人员,所以我可能会错过这里显而易见的东西......
感谢您的帮助!
var async = require('async');
//number is an integer, deepdive defines if the function is called the first time or not, cb contains the regular callback, asynCb contains the asyncJS callback
var asyncTester = function(number, deepdive, cb, asyncCb) {
//check if function should nest itself -- only on the first call
if (deepdive == true) {
var funcArray = [];
//load async with multiple calls of this function
for (var times = 2; times < 4; times++) {
funcArray.push(function(callback) {
asyncTester(times, false, null, callback);
});
}
//call async with array of functions and final callback handling
async.series(funcArray,
function(err, results) {
//return the original callback with the results from the async series
return cb(err, results);
});
}
//return the async callback when in a nested call
return asyncCb(null, number);
};
asyncTester(1, true, function(err, data) {
//expect array of results with 1, 2, 3
console.log(data);
}, null);
答案 0 :(得分:1)
感谢精简版的复制品。抛出错误是因为您正在调用空值变量作为函数。
当您自己致电asyncTester
时,您为null
提供了asyncCb
,而您为async.series
提供的功能则提供了实际的回调功能。
我已更新您的代码并添加了一些console.log
语句来说明这一点。
var async = require('async');
// number is an integer, deepdive defines if the function is called the
// first time or not, cb contains the regular callback, asynCb contains
// the asyncJS callback
var asyncTester = function(number, deepdive, cb, asyncCb) {
console.log(asyncCb); // <=== new
//check if function should nest itself -- only on the first call
if (deepdive == true) {
var funcArray = [];
//load async with multiple calls of this function
for (var times = 0; times < 4; times++) {
funcArray.push(function(callback) {
asyncTester(times, false, null, callback);
});
}
//call async with array of functions and final callback handling
async.series(funcArray,
function(err, results) {
//return the original callback with the results from
// the async series
console.log('.series callback'); // <=== new
return cb(err, results);
});
}
//return the async callback when in a nested call
return asyncCb(null, number);
};
asyncTester(
1, true,
function(err, data) { console.log(data); },
function() { console.log('all done!'); } // <=== new
);
使用console.log输出原始文件:
null
[Function]
[Function]
[Function]
[Function]
.series callback
[ 4, 4, 4, 4 ]
/Users/pmidge/workspace/personal/jstest/main.js:2079
return asyncCb(null, number);
^
TypeError: asyncCb is not a function
at asyncTester (/Users/pmidge/workspace/personal/jstest/main.js:2079:16)
at test63 (/Users/pmidge/workspace/personal/jstest/main.js:2082:5)
at Object.<anonymous> (/Users/pmidge/workspace/personal/jstest/main.js:2090:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
使用函数而不是null输出:
[Function]
[Function]
[Function]
[Function]
[Function]
.series callback
[ 4, 4, 4, 4 ]
initiating call done!
我怀疑你对这个问题的调查因为一切都按顺序发生而受到阻碍,因为(不确定这是否是故意的)你在这个例子中实际上并没有做任何异步。
我们可以强制异步执行你的内部代码,方法是将以下代码替换为你输入async.series
数组的匿名函数体:
setImmediate(function() {
asyncTester(times, false, null, callback);
});
然后控制台输出看起来像这样,并且更明显的是你的代码中有一个错误立即而不是在事件循环的未来迭代中:
null
/Users/pmidge/workspace/personal/jstest/main.js:2079
return asyncCb(null, number);
^
TypeError: asyncCb is not a function
at asyncTester (/Users/pmidge/workspace/personal/jstest/main.js:2079:16)
at test63 (/Users/pmidge/workspace/personal/jstest/main.js:2082:5)
at Object.<anonymous> (/Users/pmidge/workspace/personal/jstest/main.js:2090:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
如果您根本不想提供回调,您可以像这样保护您的退货声明:
//return the async callback when in a nested call
return asyncCb
? asyncCb(null, number)
: null;
答案 1 :(得分:0)
您的asyncTester
函数需要4个参数:
var asyncTester = function(number, deepdive, cb, asyncCb) {
但是,你只用三个来调用它:
asyncTester(1, true, function(err, data) {
所以第四个参数asyncCb
是undefined
。