I am having issues getting the value returned from a function nested in a function from another function. The code is pretty complicated, but i mocked up something here:
var request = require('request');
var _url = 'test.com/data/data.json';
function getValue() {
var val = 0;
return callFromURL(function(result){
if(result) {
// Do stuff...
console.log(result); //Returns a value
return result;
}
})
}
function callFromURL(callback) {
request(_url, function (error, response, body) {
if (!error && response.statusCode === 200) {
var service = JSON.parse(body);
return callback(service);
} else {
log.error(error);
return callback(null);
}
});
}
function test() {
getValue(); //returns undefined
}
When i step through using a debugger or console.log
statements I see that the inner return in the callback passed to callFromURL is not undefined, but the result of return callbackFromURL
is undefined
. How can I correctly return result
from the getValue()
function in this example?