1

I am new to node.js and I read a few posts about this problem but still don't understand how to solve it in my case. The correct result is printed out, but I want it to be returned or be stored in a variable. Is this possible?

    const values = async () =>
{
  const URL = 'http://www.espn.com/sports/tennis/rankings';
  const response = await request(URL);
  const $ = cheerio.load(response);
  let title = $ ('#my-players-table > div.mod-container.mod-table > div.mod-content > table > tbody').text();
  var rank = 1;
  var count = title.length;
  var words = title;
  var player = "";
  var players = [];
  for ( var i = 70; i < count; i++)
  {
    if(!characterNumber(words[i]) && words[i] != "\n")
    {
      player += words[i];
    }
    else
    {
      if(player != "" && player != '\n')
      {
        players.push(player);
        player = "";
      }
    }
  }
return players;
};
let playersRankings;
const getRankings = async () => {
 playersRankings = await values();
 console.log(playersRankings);
}
getRankings();
  • You're storing the result in `playersRankings` - otherwise, your `console.log(playersRankings)` would print `undefined`. What exactly doesn't work? Please share the piece of code where something doesn't work the way you expect it to. – shkaper Jun 07 '20 at 22:01
  • I want the values to be stored in playersRankings, the values are printed correctly, but when I use the variable outside the getRankings() function it is undefined – bestCoderEver Jun 08 '20 at 00:17

0 Answers0