Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
I can't seem to access the geolocation return value

I'm trying to get the users location using the geolocation api, in my success callback I want to return the getPosition function with the position value.

I've tried a few approaches but non of them have been successful so far, this is where I'm at right now.

function getPosition() {

    // If geolocation is available, try to get the visitor's position

    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
       console.log("Getting the position information...")
    } else {
        alert("Sorry, your browser does not support HTML5 geolocation.");
    }
    function successCallback(position) {
        return position
    }
    function errorCallback(error) {
        alert("Sorry, we can't retrieve your local weather without location permission.");
    }
};
const getLocalWeather = async () => {
    if(loading === 'loading') return
    setLoading('loading')
    console.log(getPosition())
    // this console log should return the position from getPosition
    setLoading('done')

};

The getPosition function itself works fine but I'm not able to access it's return value in the getLocalWeather function.

Any help would be appreciated, I've been stuck on this for a while.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're already familiar with async/await by the looks of things so wrap your getPosition code in a promise, and then await the response in getLocalWeather.

This code may not work in the snippet, but I've tested it locally and it logs the position without any problems.

function getPosition() {

  return new Promise((res, rej) => {
 
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(success, error);
    } else {
      console.log("Sorry, your browser does not support HTML5 geolocation.");
    }

    function success(position) {
      res(position)
    }

    function error(error) {
      console.log("Sorry, we can\'t retrieve your local weather without location permission.");
    }

  });

};

async function getLocalWeather() {
  console.log(await getPosition())
};

getLocalWeather();

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda