I have a function:
getCoordinates: function() {
geoLocation.getCurrentLocation().then(function(location) {
return "latitude: " + location.latitude + " longitude:" + location.longitude;
});
}
Which returns undefined, but when I instead do:
getCoordinates: function() {
geoLocation.getCurrentLocation().then(function(location) {
console.log("latitude: " + location.latitude + " longitude:" + location.longitude);
});
}
and run the same function I get:
"latitude: 4X.XXXXXX longitude:-12X.XXXXXXX"
I don't understand why it is returning undefined when the data must be defined or it wouldn't log to the console. Is this some kind of timing issue? What am I missing?
You are only
returning from thethencallback, not from thegetCoordinatesfunction (which in fact doesn'treturnanything, henceundefined).This is unsolvable for asynchronous callbacks in general. In your case, the best solution will be to simply return the promise that you are already creating and which will fulfill with the value you expect in the future.