0

I am using a geocoder API which returns result in a callback

data = data ? data : {} // if not given initialize to empty
geocoder.geocode('Kathmandu, Nepal' , function(err, res) {
data.lat = res[0].latitude;
data.lng = res[0].longitude;
});

and using the data object to construct a Geopoint

here = new GeoPoint(here); // data is still {} here

My question is how to use response from callback in above scenario?

Shankar Regmi
  • 854
  • 1
  • 7
  • 16
lure
  • 103
  • 2
  • 9
  • possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Aaron Dufour Aug 14 '15 at 00:04

1 Answers1

1

The geocode method is async - so any work you want to do with the response needs to be in the callback:

geocoder.geocode('Kathmandu, Nepal' , function(err, res) {
    data.lat = res[0].latitude;
    data.lng = res[0].longitude;

    here = new GeoPoint(here); //HAS TO BE IN A CALLBACK 
    //- or passed to another function from within the callback
});
tymeJV
  • 103,943
  • 14
  • 161
  • 157