Reputation: 177
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);
}
Upvotes: 0
Views: 573
Reputation: 874
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
Upvotes: 2