Skip to content

Instantly share code, notes, and snippets.

@SergeyKhval SergeyKhval/geolocation.js
Last active May 23, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.