I am creating a function to fetch the latitude and longitude using navigator object and returning these two values using an array. The problem is that when I am trying to access one of them using indexing eg:array[0] it's printing undefined and when I am accessing it in console directly everything works fine. Even if I print the whole array in code not in the console directly it also works fine. I am adding a photo for the reference in case I am unable to explain the problem. Any help will be greatly appreciatedphoto for more clear understanding statement no.13 printing undefined
async function getLocation() {
return new Promise(resolve => {
var array=new Array();
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
array.push(position.coords.latitude);
array.push(position.coords.longitude);
resolve(array);
});
}
else {
resolve(false);
}
});
}
Then just await it, like this:
let user_location=await getLocation();
Or not:
getLocation().then(result =>
console.log(result)
);