Last active
May 23, 2018 11:10
-
-
Save SergeyKhval/5ef3d4b5dd4711c5ddf6219d671f8189 to your computer and use it in GitHub Desktop.
Promised version of getting user's geolocation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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