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

202
Vistas
javascript async function of api request skips for loop

I'm trying to make an api request inside an async function and scan through the data and take specific data, yet it just skips to the return statement , here's the function:

async function getCovidDataByCountry(countryName){

    var message = '';

    const options = {
        method: 'GET',
        url: 'https://corona-virus-world-and-india-data.p.rapidapi.com/api',
        headers: {
          'X-RapidAPI-Host': 'corona-virus-world-and-india-data.p.rapidapi.com',
          'X-RapidAPI-Key': '03bc1d6a21msh5dc1f3d09f61988p1ab0a2jsn585dae537ca9',
          useQueryString: true
        }
      };
      
      request(options, function (error, response, body) {
        if (error) throw new Error(error);
        var data = JSON.parse(body);
        let length = Object.keys( data.countries_stat ).length;
        for(let i = 0;i < length; i++){
            if(data.countries_stat[i].country_name == countryName){
                message += data.countries_stat[i].toString();
            }
            
        }
        return Promise.resolve(message);
      });

}

and here's where I call it in a different script:

        let message = await chatCommandHandler.getCovidDataByCountry(msg.body.substring(8));
        msg.reply(message);
about 4 years ago · Santiago Gelvez
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