Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda