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

177
Visualizações
Cannot return a value from a function when using xhttp in javascript

Here the returned value is undefined in variable b. Because I'm trying to return a nested function value I guess. How can I return the value of x from drive function here?

function drive() {
    let xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function(){

        if(this.readyState == 4 && this.status == 200){
            var x = JSON.parse(this.response);
            console.log(x);
            return x;
            // return 1;
        }
    }
    xhttp.open("GET","https://api.openweathermap.org/data/2.5/find?q=Chennai&appid=0d0a354784e8166b7b9fee6a3a29bcc2&units=metric");
    xhttp.send();

}

document.getElementById("clickme").addEventListener("click", function () {
    let b = drive();
    console.log(b);
})
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