在process.nextTick中未定义回调



尝试递归调用同步函数来解析数组中的元素;一旦到达数组的最后一个元素,就会调用回调。但是在传递给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)返回其他函数或值。删除或评论

未定义函数result示例

var func_without_result = function(){};
console.log(func_without_result());
结果

> undefined

最新更新