ETA: not duplicate as I wasn't aware of what async was, so I didn't even know what to look for.
I'm having an issue saving location data to a variable. It seems the order of operations is not happening the way I'd expect it to. Any pointers?
Here's my code.
var test = 0;
$(document).ready(function() {
//Get location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$("#location").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
test = position.coords.longitude;
console.log(test + "a");
return test;
});
console.log(test + "g");
}
console.log(test);
});
Instead of my console log saying, [longitude]a, then [longitude]g, then [longitude]. As I would expect, reading from top down, it instead says this.
"0g"
0
"-118.36397819999999a"
What am I doing wrong?