Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!