简体   繁体   中英

Callback undefined in process.nextTick

Trying to recursively call a synchronous function to parse a element of the array; once the last element of the array is reached the callback is to be called. But the callback is said to be undefined when passed to process.nextTick where as another variable declared in the same scope as the callback prints the proper value.

Can any one explain what am I doing wrong; how can the callback be undefined, where as another variable in the same scope is accessed properly.

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); 

this string can return undefined value. Are you shure that callback(true) return other function or value. Delete it or comment.

Example of undefined function result

var func_without_result = function(){};

console.log(func_without_result());

result

> undefined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM