So I have this code:
for (let i = 0; i < array.length; i++) {
let url = array[i];
YTDL.getInfo(url, function(err, info) {
if (err) {
message.channel.send("There was an error while checking information about a video, try again soon.");
throw err;
}
songEmbed.addField("Title:", info.title);
if (i == array.length - 1) message.channel.send(songEmbed);
});
}
And the problem is that the function inside YTDL.getInfo() is called after the for loop has ended, but I need to call message.channel.send(songEmbed) only after the last iterate (see code), I tried my best to solve this by myself, using array.forEach() for example, but I cant figure out how to pass the right element index to this function inside YTDL.getInfo() I hope you understand me.