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

175
Vistas
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 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