Discordjs/voice

First time working with it,
I have a cron job running eery 5 minutes. In the callback, the bot fetches an array and loops over it. Each array has a mp3 and a channel id. Now i want to run each mp3 in its own channel.

Scenario:Bot runs the join function, a connection is created. It then listens to ready event and create a player to run audio on the connection.

Issue: when bot loops on first element, it instead of waiting for connection to be established (ready) and running the audio, just attaches the listener, then goes to next item. For next item, it again runs the join function which overrieds the previous connection according to docs.
So how would i make bot wait for connection to be ready, wait for audio to be played, since i cant even await the connection.subscribe ):

Code:

    for (const num of arr) {

        const connection = joinVoiceChannel({
            channelId: channel,
            guildId: guild.id,
            adapterCreator: guild.voiceAdapterCreator,
        });

        connection.on(VoiceConnectionStatus.Ready, async (f) => {
            
            const player = createAudioPlayer();
            player.on("error", (error) => {
                console.error(error);
            });
            const resource = createAudioResource("./test.mp3");
            player.play(resource);

            // Play "track.mp3" across two voice connections
            connection.subscribe(player);

            
        });
Was this page helpful?