gpt4 book ai didi

javascript - 类型错误 : Callback is not a function with asyncJS in nodeJS

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:47 24 4
gpt4 key购买 nike

我想编写一个通过多个同步调用来调用自身的函数(此处的代码只是流程的一个精简示例)。问题是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);

最佳答案

感谢您的精简重现。抛出错误是因为您将空值变量作为函数调用。

当您自己调用asyncTester时,您为asyncCb提供null,而您为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);
});

然后控制台输出如下所示,更明显的是,您的代码中存在错误,该错误立即运行,而不是在事件循环的 future 迭代中运行:

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;

关于javascript - 类型错误 : Callback is not a function with asyncJS in nodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36944604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com