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

115
Visualizações
axios function is running after other lines of code

My Code:

var latlng = {lat: 7, lng: 7};

function findLatLng() {
    var testLocation = '350 Victoria St, Toronto';

    axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
        params: {
            address: testLocation,
            key: 'MY KEY',
        }
    })
    .then(function(response) {
        console.log(response.data.results[0].geometry.location);
        return response.data.results[0].geometry.location;
    })
    .catch(function(error) {
        console.log(error);
    });
}

latlng = findLatLng();
console.log(latlng);

In the console, latlng is logged as undefined, but the console.log(response.data.results[0].geometry.location) is logged with the proper value;
it is also logged afterwards even though it should have executed first.

I think this is because the code continues despite findLatLng() not being done with the request but I'm not sure. When I check the value of latlng in the console afterwards, it becomes the proper value.

What is the issue and how do I fix this please?

about 4 years ago · Juan Pablo Isaza
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