I'm trying to convert this code into promise (The one that i commented //). My goal is to print the user's location (longitude and latitude) but I'm having a hard time figuring it out, on how to convert this into Promise. (Sorry for my english)
// const getUserLocation = () => {
// if (navigator.geolocation) {
// navigator.geolocation.getCurrentPosition(succes, error);
// } else {
// console.log('Your browser does not support geolocation');
// }
// }
// const succes = (positon) => {
// console.log(positon.coords.latitude)
// console.log(positon.coords.longitude)
// }
// const error = (err) => {
// console.log('The User have denied the request for Geolocation.');
// }
// getUserLocation();
const getUserLocation = () => {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
resolve(navigator.geolocation.getCurrentPosition);
} else {
reject('The User have denied the request for Geolocation.');
}
})
}
getUserLocation()
.then((response) => {
console.log(response.coords.longitude);
console.log(response.coords.latitude);
})
.catch((err) => {
console.log(err);
})