I am currently trying to return an array that is modified within a function. The function is using the "billboard-hot-100" npm.
const { getChart } = require('billboard-top-100');
artistArrayPopular = [];
imageArrayPopular = [];
getChart('artist-100', '', (err, chart) => {
if (err) console.log(err);
for (let i = 0; i < 100; i++) {
artistArrayPopular.push(chart.songs[i].artist)
imageArrayPopular.push(chart.songs[i].cover)
}
console.log("test 1: " + artistArrayPopular);
return artistArrayPopular;
});
console.log("test 2: " + artistArrayPopular)
The "test 1" console log returns the properly appended array after about a 2 second delay, but the "test 2" not only fires instantly, but also returns a blank array. Plus, the "return" function seems to return a blank array as well.
Can someone troubleshoot this and tell me how to get the appended array to be accessible? I have a feeling it has something to do with asynchronous calls, but I cannot get it to work no matter what I do.