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

203
Visualizações
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 à 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