I have the code:
country: (origin) ->
@geocoder = new google.maps.Geocoder
@geocoder.geocode(
'latLng': origin,
(results, status) =>
if status is google.maps.GeocoderStatus.OK
return results[6]
else alert("Geocode was not successful for the following reason: " + status);
)
I am calling it in backbone.js as:
test = @country(origin)
console.log(test)
As a test I am using the console.log. However I am getting an:
undefined
response, as the country function is not returning anything. I know that results[6] has data in it, as I can do a conolse.log there and it returns.
How can I make the country function return result[6] when called?
I don’t know that API, per se, but it looks like it’s asynchronous, which means you cannot get the function to return the value. Instead, you’ll have to pass in a continuation function that deals with the result when it becomes available.
To use this, simply craft a function that knows what to do with the result and pass it to the
countryfunction: