I have created a new promise function to set the asynchronous geolocation as a global function.
function getUsrLoc() {
return new Promise(function(resolve, reject) {
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(resolve)
} else {
reject('Geolocation is not supported by this browser or OS');
}
});
}
Now I have created a function to push the coordinates to a new array whilst returning the new variable with the expected array items once I run the .then method.
//push coordinates into new array
function showPosition(position) {
var coordinates = new Array();
coordinates.push(Math.floor(position.coords.latitude))
coordinates.push(Math.floor(position.coords.latitude))
console.log(coordinates);
return coordinates;
}
Now I can run the .then method on the getUsrLoc() function and plug in the showPosition function as a parameter.
getUsrLoc().then(showPosition);
Now on running, I the coordinates are printed to the console (after the browser prompt) but are not returning the new variable coordinates.
coordinates;
//undefined
What am I missing here?