title: Adding a Promise to Promise.all()
body: <p>I've got an api call that sometimes returns paged responses. I'd like to automatically add these to my promises so I get the callback once all the data has arrived.</p>
  
  <p>This is my attempt. I'd expect the new promise to be added and Promise.all to resolve once that is done.</p>
  
  <p>What actually happens is that Promise.all doesn't wait for the second request. My guess is that Promise.all attaches "listeners" when it's called.</p>
  
  <p>Is there a way to "reintialize" Promise.all()?</p>
  
  <pre><code>function testCase (urls, callback) {
      var promises = [];
      $.each(urls, function (k, v) {
          promises.push(new Promise(function(resolve, reject) {
              $.get(v, function(response) {
                  if (response.meta &amp;&amp; response.meta.next) {
                      promises.push(new Promise(function (resolve, reject) {
                          $.get(v + '&amp;offset=' + response.meta.next, function (response) {
                              resolve(response);
                          });
                      }));
                  }
                  resolve(response);
              }).fail(function(e) {reject(e)});
          }));
      });
  
      Promise.all(promises).then(function (data) {
          var response = {resource: []};
          $.each(data, function (i, v) {
              response.resource = response.resource.concat(v.resource);
          });
          callback(response);
      }).catch(function (e) {
          console.log(e);
      });
  }   
  </code></pre>
  
  <p>Desired flow is something like:</p>
  
  <ol>
  <li>Create a set of promises.</li>
  <li>Some of the promises spawn more promises.</li>
  <li>Once all the initial promises and spawned promises resolve, call the callback.</li>
  </ol>