Return value from nested function in Javascript

I have a function that installs as follows

function mainFunction() { function subFunction() { var str = "foo"; return str; } } var test = mainFunction(); alert(test); 

In its logic, this warning should return 'foo', but instead returns undefined. What am I doing wrong?

UPDATE : here is my actual code (this is a function for reverse geocoding with Google APIs)

 function reverseGeocode(latitude,longitude){ var address = ""; var country = ""; var countrycode = ""; var locality = ""; var geocoder = new GClientGeocoder(); var latlng = new GLatLng(latitude, longitude); return geocoder.getLocations(latlng, function(addresses) { address = addresses.Placemark[0].address; country = addresses.Placemark[0].AddressDetails.Country.CountryName; countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode; locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName; return country; }); } 
+43
javascript function return nested return-value
Apr 10 2018-10-10T00:
source share
3 answers

you need to call the function before it can return something.

 function mainFunction() { function subFunction() { var str = "foo"; return str; } return subFunction(); } var test = mainFunction(); alert(test); 

Or:

 function mainFunction() { function subFunction() { var str = "foo"; return str; } return subFunction; } var test = mainFunction(); alert( test() ); 

for your actual code. Return should be outside, in the main function. The callback is called somewhere inside the getLocations method, and therefore its return value is not returned inside your main function.

 function reverseGeocode(latitude,longitude){ var address = ""; var country = ""; var countrycode = ""; var locality = ""; var geocoder = new GClientGeocoder(); var latlng = new GLatLng(latitude, longitude); geocoder.getLocations(latlng, function(addresses) { address = addresses.Placemark[0].address; country = addresses.Placemark[0].AddressDetails.Country.CountryName; countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode; locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName; }); return country } 
+53
Apr 10 '10 at 2:17
source share

Right The function you pass getLocations () will not be called until the data is available, so returning the "country" before setting it will not help you.

The way you need to do this is to execute the function that you pass to geocoder.getLocations (), in fact to do what you wanted to do with the return values.

Something like that:

 function reverseGeocode(latitude,longitude){ var geocoder = new GClientGeocoder(); var latlng = new GLatLng(latitude, longitude); geocoder.getLocations(latlng, function(addresses) { var address = addresses.Placemark[0].address; var country = addresses.Placemark[0].AddressDetails.Country.CountryName; var countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode; var locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName; do_something_with_address(address, country, countrycode, locality); }); } function do_something_with_address(address, country, countrycode, locality) { if (country==="USA") { alert("USA A-OK!"); // or whatever } } 



If you want to do something different every time you get the location, then pass the function as an optional reverseGeocode parameter:

 function reverseGeocode(latitude,longitude, callback){ // Function contents the same as above, then callback(address, country, countrycode, locality); } reverseGeocode(latitude, longitude, do_something_with_address); 

If this looks a little dirty, you can take a look at something like the “Snooze” function in Dojo, which makes the chain between the functions a little clearer.

+3
Apr 10 '10 at 3:10
source share

Just FYI, the geocoder is asynchronous, so the accepted answer while the boolean is not working in this case. I would rather have an external object that acts like your updater.

 var updater = {}; function geoCodeCity(goocoord) { var geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'latLng': goocoord }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { updater.currentLocation = results[1].formatted_address; } else { if (status == "ERROR") { console.log(status); } } }); }; 
+1
Oct 26 '14 at 3:49
source share



All Articles