Search code examples
javascriptreactjsgoogle-chromecapacitor

Geolocation.requestPermissions not implemented for browser and error is always uncaught?


import { Geolocation } from "@capacitor/geolocation";

  const request_perm = () => {
    try {
      const stuff = Geolocation.requestPermissions();
      console.log(stuff, "stuff");
    } catch (err) {
      console(err, "err");
    }
  };

as you can see, the call to Geolocation.requestPermissions is wrapped inside a try block, so I don't see why it is still an uncaught error:

enter image description here

The error goes away if use the await like so:

  const request_perm = async () => {
    try {
      const stuff = await Geolocation.requestPermissions();
      console.log(stuff, "stuff");
    } catch (err) {
      console.log(err, "err");
    }
  };

It is still not implemented, however the error is now caught by the catch block.


Solution

  • You have to understand how javascript works for this question. here is a great explanation about js event loop & callback queue.

    In short, the Gelocation.requestPermissions() function returns a Promise, which means it is an async function. If it is not called in async function & await clause is not in front of the call, the javascript engine won't wait until the promise becomes settled, running the rest of the functions.

    Which means console.log below the requestPermissions call will be run and try-catch clause will end before the promise becomes settled -> goes inside task queue(microstask queue actually) -> waits for the call stack to be empty