title: Youtube API with JavaScript - Show all videos from a playlist
body: <p>I'm trying to get all 137 videos from a Youtube playlist with the following code:</p>
  
  <pre><code>function loadVideos() {
  
    let pagetoken = '';
    let resultCount = 0;
    const mykey = "***********************************";
    const playListID = "PLzMXToX8Kzqggrhr-v0aWQA2g8pzWLBrR";
  
    const URL =  `https://www.googleapis.com/youtube/v3/playlistItems?
  part=snippet
  &amp;maxResults=50
  &amp;playlistId=${playListID}
  &amp;key=${mykey}`;
  
  
    fetch(URL)
      .then(response =&gt; {
        return response.json();
      })
      .then(function(response) {
  
        resultCount = response.pageInfo.totalResults;
        console.log("ResultCount: " + resultCount);
  
        pagetoken = response.nextPageToken;
        console.log("PageToken: " + pagetoken);
  
        resultCount = resultCount - 50;
        console.log("ResultCount: " + resultCount);
  
        while (resultCount &gt; 0) {
  
          const URL = `https://www.googleapis.com/youtube/v3/playlistItems?
  part=snippet
  &amp;maxResults=50
  &amp;playlistId=${playListID}
  &amp;key=${mykey}
  &amp;pageToken=${pagetoken}`;
  
          fetch(URL)
            .then(response =&gt; {
              return response.json();
            })
            .then(function(response) {
              pagetoken = response.nextPageToken ? response.nextPageToken : null;
              console.log("PageToken: " + pagetoken);
            });
          resultCount = resultCount - 50;
        }
      })
      .catch(function(error) {
        console.log("Looks like there was a problem: \n", error);
      });
  } // End of loadVideos function
  
  // Invoking the loadVideos function
  loadVideos();
  </code></pre>
  
  <p>The first 50 videos get loaded
  The second 50 videos get loaded too
  But instead of loading the remaining 37 videos from the list, my script loads the previous 50 videos again. </p>
  
  <p>It seems that the <strong>pagetoken</strong> doesn't get updated for the third request.</p>
  
  <p>What is wrong with my code?</p>