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

159
Vistas
Facing problem when return an array from a function in JavaScript

I am creating a function to fetch the latitude and longitude using navigator object and returning these two values using an array. The problem is that when I am trying to access one of them using indexing eg:array[0] it's printing undefined and when I am accessing it in console directly everything works fine. Even if I print the whole array in code not in the console directly it also works fine. I am adding a photo for the reference in case I am unable to explain the problem. Any help will be greatly appreciatedphoto for more clear understanding statement no.13 printing undefined

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

0

async function getLocation() {
        return new Promise(resolve => {
            var array=new Array();
            if(navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(position => {
                    array.push(position.coords.latitude);
                    array.push(position.coords.longitude);
                    resolve(array);
                });
            }
            else {
                resolve(false);
            }
        });
    }

Then just await it, like this:

let user_location=await getLocation();

Or not:

getLocation().then(result =>
        console.log(result)
        );
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