title: How to execute a function only after a request.execute to gapi.client.youtube has completed?
body: <p>I am trying to get results from multiple pages of a youtube playlist. The problem is that I cannot get my second request (for the second page) to execute only after I have received the data from the first one. I've tried promises, deferrals, callbacks, and nothing seems to work.</p>
  
  <pre><code>function requestVideoPlaylist(playlistId, pageToken, pages) {
    $('#video-container').html('');
    var requestOptions = {
      playlistId: playlistId,
      part: 'snippet',
      maxResults: 10
    };
    if (typeof(pages)==='undefined') {
      pages = 2;
    }
  
    var i = 0
    while (i++ &lt; pages){
      var request = gapi.client.youtube.playlistItems.list(requestOptions);
      request.execute(function(response) {
  
        nextPageToken = response.result.nextPageToken;
  
        var playlistItems = response.result.items;    
        console.log(playlistItems)
  
        if (playlistItems) {
          $.each(playlistItems, function(index, item) {
            displayResult(item.snippet);
          });
        } else {
          $('#video-container').html('Sorry you have no uploaded videos');
        }
      }).done(console.log('test'));
  
      if (nextPageToken) {
        console.log(nextPageToken)
        requestOptions.pageToken = nextPageToken;
      }
    }       
  }
  </code></pre>
  
  <p>In this example I get a message <em>'Uncaught TypeError: Cannot read property 'done' of undefined'</em> for the <em>.done(console.log('test))</em> and nothing from <em>console.log(nextPageToken)</em>, as that part also executes before the response is received.</p>