How to Promisify geolocation API to get current position using JavaScript ?

In this article, we are going to use Promisify geolocation API into Promise-based API.
Prerequisite: JavaScript PromiseĀ
Approach: As we know the navigator.geolocation.getCurrentPosition is a callback-based API, so we can easily convert it into a Promise-based API. To promisify the geolocation API, we will get the userās current position from the browser. We will either resolve the promise or reject the promise based upon, either we get the current position.
Example:Ā
HTML
<!DOCTYPE html><html>Ā
<head>Ā Ā Ā Ā <meta charset="UTF-8" />Ā
Ā Ā Ā Ā <style>Ā Ā Ā Ā Ā Ā Ā Ā body {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā text-align: center;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā h1 {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā color: green;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā h5 {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā color: black;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā #zambiatek {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā font-size: 16px;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā font-weight: bold;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā #gfg {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā color: green;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā font-size: 20px;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā font-weight: bold;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā </style></head>Ā
<body>Ā Ā Ā Ā <h1>zambiatek</h1>Ā Ā Ā Ā <p id="zambiatek"></p>Ā
Ā
Ā Ā Ā Ā <button onClick="getLocation()">Get My Location</button>Ā Ā Ā Ā <p id="gfg"></p>Ā
Ā
Ā Ā Ā Ā <script>Ā Ā Ā Ā Ā Ā Ā Ā let s = `Promisifying the Geo Location API`;Ā Ā Ā Ā Ā Ā Ā Ā document.getElementById("zambiatek").innerHTML = `<p>${s}</p>`;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Logic start hereĀ Ā Ā Ā Ā Ā Ā Ā let getLocationPromise = () => {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return new Promise(function (resolve, reject) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Promisifying the geolocation APIĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā navigator.geolocation.getCurrentPosition(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (position) => resolve(position),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (error) => reject(error)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā );Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā });Ā Ā Ā Ā Ā Ā Ā Ā };Ā
Ā Ā Ā Ā Ā Ā Ā Ā function getLocation() {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā getLocationPromise()Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .then((res) => {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If promise get resolvedĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā const { coords } = res;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.getElementById("gfg").innerHTML = `Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <p>Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <strong>You are Located at :</strong>Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā </p>Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <h5>latitude : ${coords.latitude}</h5>Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <h5>longitude : ${coords.longitude}</h5>Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā `;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā })Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .catch((error) => {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If promise get rejectedĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.getElementById("gfg")Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .innerHTML = `<p>${error}</p>`;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā });Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā </script></body>Ā
</html> |
Output:
Promisify the geoLocation API
Example 2: We can even simplify the previous code, to make it better. As we know the navigator.geolocation.getCurrentPosition(callback, error) automatically calls the call-back function and passes the position.
HTML
<!DOCTYPE html><html>Ā
<head>Ā Ā Ā Ā <meta charset="UTF-8" />Ā Ā Ā Ā <style>Ā Ā Ā Ā Ā Ā Ā Ā body {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā text-align: center;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā h1 {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā color: green;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā h5 {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā color: black;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā #zambiatek {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā font-size: 16px;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā font-weight: bold;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā #gfg {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā color: green;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā font-size: 20px;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā font-weight: bold;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā </style></head>Ā
<body>Ā Ā Ā Ā <h1>zambiatek</h1>Ā Ā Ā Ā <p id="zambiatek"></p>Ā
Ā
Ā Ā Ā Ā <button onClick="getLocation()">Get My Location</button>Ā Ā Ā Ā <p id="gfg"></p>Ā
Ā
Ā Ā Ā Ā <script>Ā Ā Ā Ā Ā Ā Ā Ā let s = `Promisifying the Geo Location API`;Ā Ā Ā Ā Ā Ā Ā Ā document.getElementById("zambiatek")Ā Ā Ā Ā Ā Ā Ā Ā .innerHTML = `<p>${s}</p>`;Ā
Ā Ā Ā Ā Ā Ā Ā Ā let getLocationPromise = () => {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return new Promise(function (resolve, reject) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Automatically passes the positionĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // to the callbackĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā navigator.geolocationĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .getCurrentPosition(resolve, reject);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā });Ā Ā Ā Ā Ā Ā Ā Ā };Ā Ā Ā Ā Ā Ā Ā Ā function getLocation() {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā getLocationPromise()Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .then((res) => {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If promise get resolvedĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā const { coords } = res;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.getElementById("gfg").innerHTML = `Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <p>Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <strong>You are Located at :</strong>Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā </p>Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <h5>latitude : ${coords.latitude}</h5>Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <h5>longitude : ${coords.longitude}</h5>Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā `;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā })Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .catch((error) => {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If promise get rejectedĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.getElementById("gfg").innerHTMLĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = `<p>${error}</p>`;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Console.error(error);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā });Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā </script></body>Ā
</html> |
Output:
Promisify the geoLocation API



