1

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);
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • The callback function runs asynchronously. Nothing waits for it, and the return value is is ignored. – Barmar Apr 12 '22 at 17:52

0 Answers0