title: Programmatically Obtain All YouTube Videos Belonging to a User
body: <p>Utilizing the YouTube Data API we can make a query to to obtain the first 50 (maximum amount of results obtainable with a single query) videos belonging to a user with the following request: <code>https://www.googleapis.com/youtube/v3/search?key={access_key}&amp;channelId={users_id}&amp;part=id&amp;order=date&amp;maxResults=50&amp;type=video</code></p>
  
  <p>If there are more than 50 videos, then the resulting JSON will have a <code>nextPageToken</code> field, to obtain the next 50 videos we can append <code>&amp;pageToken={nextPageToken}</code> to the above request, producing the next page of 50 videos. This is repeatable until the <code>nextPageToken</code> field is no longer present.</p>
  
  <p>Here is a simple JavaScript function I wrote using the fetch API to obtain a <strong>single</strong> page of videos, specified by the <code>nextPageToken</code> parameter (or lack thereof).</p>
  
  <pre><code>function getUploadedVideosPage(nextPageToken) {
      return new Promise((resolve, reject) =&gt; {
          let apiUrl = 'https://www.googleapis.com/youtube/v3/search?key={access_key}&amp;channelId={users_id}&amp;part=id&amp;order=date&amp;maxResults=50&amp;type=video';
          if(nextPageToken)
              apiUrl += '&amp;pageToken=' + nextPageToken;
  
          fetch(apiUrl)
          .then((response) =&gt; {
              response.json()
              .then((data) =&gt; {
                  resolve(data);
              });
          });
      });
  }
  </code></pre>
  
  <p>Now we need a wrapper function that will iteratively call <code>getUploadedVideosPage</code> for as long as we have a <code>nextPageToken</code>. Here is my 'working' albeit dangerous (more on this later) implementation.</p>
  
  <pre><code>function getAllUploadedVideos() {
      return new Promise((resolve, reject) =&gt; {
          let dataJoined = [];
          let chain = getUploadedVideosPage();
  
          for(let i = 0; i &lt; 20000; ++i) {
              chain = chain
              .then((data) =&gt; {
                  dataJoined.push(data);
  
                  if(data.nextPageToken !== undefined)
                      return getUploadedVideosPage(data.nextPageToken);
                  else
                      resolve(dataJoined);
              });
          }
      });
  }
  </code></pre>
  
  <p>The 'dangerous' aspect is the condition of the for loop, theoretically it should be infinite <code>for(;;)</code> since we have no predefined way of knowing exactly how many iterations to make, and the only way to terminate the loop should be with the <code>resolve</code> statement. Yet when I implement it this way it truly is infinite and never terminates.</p>
  
  <p>Hence why I hard coded 20,000 iterations, and it seems to work but I don't trust the reliability of this solution. I was hopping somebody here can shed some light on how to go about implementing this iterative Promise chain that has no predefined terminating condition.</p>