I am unable to get the value of the response outside the callback code. It returns undefined outside whereas in the callback it is giving proper result.
function doCall(urlString, callback) {
request.get(
urlString,
null,
null,
(err, data, result) => {
var statusCode = result.statusCode;
return callback(data);
}
);
}
const apiResponse = doCall(urlString, function(response) {
console.log('***************************' + response); //Prints correct result
return JSON.parse(response);
});
console.log('+++++++++++++++++++++++++' + apiResponse); //Prints undefined
doCalldoes not return anything, so affecting its return "value" toapiResponsesets its value toundefined.