How to Promisify geolocation API to get current position using JavaScript ? Last Updated : 03 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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; } #geeks { font-size: 16px; font-weight: bold; } #gfg { color: green; font-size: 20px; font-weight: bold; } </style> </head> <body> <h1>GeeksforGeeks</h1> <p id="geeks"></p> <button onClick="getLocation()">Get My Location</button> <p id="gfg"></p> <script> let s = `Promisifying the Geo Location API`; document.getElementById("geeks").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; } #geeks { font-size: 16px; font-weight: bold; } #gfg { color: green; font-size: 20px; font-weight: bold; } </style> </head> <body> <h1>GeeksforGeeks</h1> <p id="geeks"></p> <button onClick="getLocation()">Get My Location</button> <p id="gfg"></p> <script> let s = `Promisifying the Geo Location API`; document.getElementById("geeks") .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 Comment More infoAdvertise with us Next Article How to Promisify geolocation API to get current position using JavaScript ? A AStream26 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Properties JavaScript-Questions Similar Reads How to get protocol, domain and port from URL using JavaScript ? The protocol, domain, and port of the current page can be found by two methods: Method 1: Using location.protocol, location.hostname, location.port methods: The location interface has various methods that can be used to return the required properties. The location.protocol property is used to return 2 min read How to Get the Current URL using JavaScript? Here are two different methods to get the current URL in JavaScript.1. Using Document.URL PropertyThe DOM URL property in HTML is used to return a string that contains the complete URL of the current document. The string also includes the HTTP protocol such as ( http://).Syntaxdocument.URLReturn Val 1 min read How to get Geolocation using PHP-cURL from IP Address ? Geolocation refers to the identification of the geographical location of a user or computer device. In this article, we will create a web page where the user can enter the IP Address of any device, and then the server will provide Geolocation of the IP address fetching the following details using t 2 min read How to get city name by using geolocation ? In this article, we will learn how to get the city name of the user using reverse geocoding. We will be using a 3rd party API service provider(Location IQ) to help us to convert the latitude and longitude we receive into an address. Steps: Get user coordinates. Get city name. Note: A LocationIQ acco 3 min read Get current Geolocation of user using React-Geolocated In some situations, applications need to access the current location properties of the user. Location properties include latitude, longitude, and some more about the current location of the user. From the Location properties, the user can know the current geolocation of the user.Approach:Geolocation 2 min read How to get client IP address using JavaScript? Imagine your computer as your apartment. Just like each apartment has a unique address for receiving mail, your computer has an IP address that helps it receive information from the internet. This IP address acts like a label that identifies your specific device on the vast network of computers, ens 2 min read How to set location and location.href using JavaScript ? In this article, we will set the location and location.href using Javascript.Both location and location.href are used to set or return the complete URL of your current page. They return a string that contains the entire URL with the protocol.Syntax:location = "https://www.geeksforgeeks.org";orlocati 1 min read How to Convert Date to Another Timezone in JavaScript? Converting a date to another timezone in JavaScript means adjusting the local date and time to match the time in a different timezone. This ensures that the displayed time aligns with the selected timezone, often using built-in methods or external libraries.1. Using Intl.DateTimeFormat() and format( 2 min read How to Redirect to Another Webpage using JavaScript? JavaScript can redirects the users from current page to another webpage (different URL) with and without requiring any manual action (like clicking a link). To redirect to another weboage in JavaScript we need to manipulate the window.location object and it will allow us to navigate programmatically 2 min read How to get the client timezone offset in JavaScript? The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes. This offset is changed by dividing by 60 and negating the result. Not 1 min read Like