Browser Geolocation API not throwing error or respecting timeout

This has felt like black magic to me for some time, and I’m hoping one of you can help clear this up. I have a geolocation function in JS that goes as follows:

// User Position
export function getPosition(options) {
  return new Promise(function (resolve, reject) {
    function success(pos) {
      var crd = pos.coords;
      console.log('we have succeeded with ' + crd)
      resolve(pos);
    }

    function failure(err) {
      console.warn(`ERROR(${err.code}): ${err.message}`);
      reject();
    }

    var options = {
      enableHighAccuracy: true,
      timeout: 5500,
      maximumAge: 6000
    };

    // debugger;
    navigator.geolocation.getCurrentPosition(success, failure, options);
  });
};

and it is called within an async function with:

let position = await getPosition();

I have identified two cases where the promise is never resolved, somewhat predictably even with a timeout set.

  • Browser is updated in iOS or MacOS and the new privacy settings revoke location access.
  • Opened from a link in safari or chrome on iOS device

The getPosition() function will hang indefinitely, or far exceeding the timeout duration before the user is prompted.

Am I missing something in my code, or is this specific to new privacy settings with Apple operating systems where the geolocation API is falling on it’s face?

25 thoughts on “Browser Geolocation API not throwing error or respecting timeout”

Leave a Comment