Skip to content

Instantly share code, notes, and snippets.

@SergeyKhval
Last active May 23, 2018 11:10
Show Gist options
  • Save SergeyKhval/5ef3d4b5dd4711c5ddf6219d671f8189 to your computer and use it in GitHub Desktop.
Save SergeyKhval/5ef3d4b5dd4711c5ddf6219d671f8189 to your computer and use it in GitHub Desktop.
Promised version of getting user's geolocation
function getPositionPromised() {
function successCb(resolve) {
return position => resolve(position);
}
function errorCb(reject) {
return () => reject('Could not retrieve geolocation');
}
return new Promise((resolve, reject) => {
if (window.navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCb(resolve), errorCb(reject));
} else {
return reject('No geolocation support');
}
})
}
//You can use it like this:
getPositionPromised()
.then(position => {/*do something with position*/})
.catch(() => {/*something went wrong*/})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment