The getLocation()
function that should retrieve location in 3 attempts returns undefined
instead. navigator.geolocation.getCurrentPosition()
returns the correct position, but the problem is in the promise handling.
The problem is apparently that I am calling a promise inside promise. I am not allowed to use the await
keyword inside geolocate()
already declared as async
.
Original call:
var getLocationPromise = this.getLocation();
// Do something...
location = await getLocationPromise;
getLocation()
:
async getLocation() {
return new Promise((resolve, reject) => {
var geolocate;
for (let i=0; i<3; i++) {
geolocate = this.geolocate();
try {
var location = geolocate;//CAN'T USE AWAIT INSIDE ASYNC...
resolve(location);
} catch(err) {
continue;
}
}
reject("Max geolocation attempts");
});
}
geolocate()
:
async geolocate() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
resolve(position);
},
(err) => {
reject(err);
},
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
});
}