gpt4 book ai didi

javascript - process.nextTick 中未定义回调

转载 作者:行者123 更新时间:2023-12-02 17:21:48 26 4
gpt4 key购买 nike

尝试递归调用同步函数来解析数组的元素;一旦到达数组的最后一个元素,就会调用回调。但是,当传递给 process.nextTick 时,回调被认为是未定义的,因为在与回调相同的范围内声明的另一个变量会打印正确的值。

任何人都可以解释一下我做错了什么吗?回调怎么可能是未定义的,而同一范围内的另一个变量可以正确访问。

someobject.prototype.launch = function (callback) {

if (!this.config.children || this.config.children.length <= 0)
throw new Error('No children found');
var callCounter = 1;
var _callback = function () {
callback(true)
};
console.log(_callback); // prints [Function] as expected
function create(i) {
//some logic to process
//the 'i' element
if (callCounter == stopAt) {
var _callback = callback(true);
//process.nextTick(_callback); //this too doesnt work as _callback is undefined
process.nextTick(function () {
console.log(callCounter + ' ' + _callback);
// the callback is undefined where as callCounter holds the proper value.
});
}else {
//process.nextTick( (create.bind(this))(callCounter++));
(create.bind(this))(callCounter++)
}
(create.bind(this))(0);
}

最佳答案

var _callback = callback(true); 

该字符串可以返回未定义的值。您确定回调(true)返回其他函数或值吗?删除它或评论。

未定义函数结果示例

var func_without_result = function(){};

console.log(func_without_result());

结果

> undefined

关于javascript - process.nextTick 中未定义回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23873111/

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