I am trying to return value from function inside function in a nodejs app. Its always coming out to be undefined.
var geo = {
list: function(callback){
var returnval;
gapi.getcodes(function(error, data) {
if (error) {
console.log(error);
} else {
returnval = data;
console.log(returnval);
}
});
callback(returnval);
}
}
var geocodes = geo.list(function(){});
console.log("Value of geocodes: "+geocodes);
I know gapi.getcodes is asynchronous function and i read on another thread that I should be passing callback function to get the return value. But still the value of geocodes is still coming out to be undefined.
When the code runs i see the value returned in
console.log(returnval)
. How do I get this value to be stored in a var?
I am not sure what is wrong. Please help I am trying to learn javascript and object style pattern.