1

Here's what I need to do, I need to enqueue an entire youtube playlist in order, so i need to do something like this

const track_names = await sp.get_playlist_track_names(link);

for (let i = 0; i < track_names.length; i++){
  queue[last_index] = await yt.get_video(track_names[i]);
}

So as you could imagine this is waaaaay to slow, because if i try to enqueue a 100 songs playlist it doesn't start playing until i get all songs info.

Is there a faster way to do it? Or to play the first song and let the program execute the function play_song() while my function get_tracks_info() getting the remaining songs.

maazadeeb
  • 5,922
  • 2
  • 27
  • 40
Shuls
  • 35
  • 1
  • 7
  • Voting to reopen because OP doesn't want to wait for all promises to resolve, they specifically want to do something as soon as the first promise resolves – Nick Jan 25 '21 at 02:06
  • use observer api to watch variable change, and do not await any promise. – 王仁宏 Jan 25 '21 at 02:15
  • 2
    Put a `if (i == 0) play_song()` in the loop? – Bergi Jan 25 '21 at 02:41
  • 1
    How about using [Promise.race](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) to wait for any of the songs to finish download -> and then start playing and download the rest? – Mark Jan 25 '21 at 08:39

1 Answers1

0

If it were the track coming from my server, I would do it with streaming.

const trackList = await getTheListOfYourTracks();

let curTrackIndex = -1;

Make an api request to get the data of current track in chunks

function playNextTrack() {
  if (curTrackIndex <= trackList.length - 1) {
    currentTrack = trackList[curTrackIndex + 1];
    // Make API call here
    // After finish, call this function again to play next track
    ++curTrackIndex;
    playNextTrack();
  } else {
  // Do whatever after tracklist ends
  }
}

playNextTrack();

Then make another api request as soon as the first track is over

This way you don't have to wait for anything and it will faster

Abhishek Pankar
  • 723
  • 8
  • 26