1

I'm a Node noob, trying to write this script to query the Billboard Hot 100 API for ten years of top-10 songs, extracting the top ten songs for each year.

I want to console.log the results of the loop -- but the console.log statement is executed asynchronously before the loop's done ...

How would one rewrite this so the console.log happens after the loop is done?

Yeah async baffles me.

var billboard = require("billboard-top-100").getChart;
var lyricsText = "List of songs: \n\n";

function getTheSongs() {
  for (let q = 1958; q < 1968; q++) {
    let c = q.toString() + '-07-01';
    billboard('hot-100', c, function(songs) {
      for (let u = 0; u < 10; u++) {
        lyricsText += songs[u].title + ", " + songs[u].artist + "\n";
      }
    }
  )};
  console.log(lyricsText);
}

getTheSongs();

0 Answers0