I've a little problem with nodeJS and their asynchron functions. I need a function which does a GET request for getting some API data and then commits some data back into 2 variables to the function call for further use. But the problem is, that I can't use the response data outside the asynchron request function to return some data.
Is there any possibility to realize that? If not how can I do something like that?
var geoData = function(address){
// Google API Key
apikey = 'XXX';
// google API URL for geocoding
var urlText = 'https://maps.googleapis.com/maps/api/geocode/json?address='
+ encodeURIComponent(address)+'&key=' + apikey;
request(urlText, function (error, response, body) {
if (!error && response.statusCode == 200)
jsonGeo = JSON.parse(body);
console.log(jsonGeo.results[0].geometry.location);
}
})
// Variable jsonGeo isn't declared here
latitude = jsonGeo.results[0].geometry.location.lat;
longitude = jsonGeo.results[0].geometry.location.lng;
return [latitude,longitude];
};
Thanks so much and sorry for my bad english!
returnin continuation passing style; use a callback.