1

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!

8
  • Why don't you just add "latitude = jsonGeo.results[0].geometry.location.lat; longitude = jsonGeo.results[0].geometry.location.lng;" into your request callback function ? Commented Jul 18, 2015 at 23:05
  • You cannot do that. Forget about return in continuation passing style; use a callback. Commented Jul 18, 2015 at 23:06
  • I've done this before, but it creates just undefined results at function call. Commented Jul 18, 2015 at 23:07
  • @elclanrs: Could you give me a little example for this way? Thanks Commented Jul 18, 2015 at 23:09
  • 1
    @km65 In any reason you can deasync you async function. For example, use github.com/abbr/deasync Commented Jul 18, 2015 at 23:43

1 Answer 1

1

Instead of of returning something use a callback for geoData which will do the necessary task .

var geoData = function(address, callback){
    // 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);
            latitude = jsonGeo.results[0].geometry.location.lat;
            longitude = jsonGeo.results[0].geometry.location.lng;
            callback([latitude,longitude]);
        }
    })    
};

use it like this

geoData('myaddress', function(arr){
    console.log(arr[0], arr[1]);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your fast and helpful answer! That's the solution.
This async world is really a doom loop ;-) But this way it works! Thanks
Indeed, instead of fighting the async nature of node.js, adapt, adopt and enjoy it!
@Dirk: I'll try it ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.