title: Resolve deferred only after multiple ajax calls has returned all data - offset / take
body: <p>I am making a call to a web service that returns a collection of books, with each request containing a 'MoreItems' flag to indicate if there is more data for the request to return. This is because the web service returns data in a paged fashion limited to 100, so the client must request with an offset value.</p>
  
  <p>e.g:</p>
  
  <pre><code>   var getBooksFromWebserver = function (param) {
      return {
          uri: 'https://webservice.com/Books/GetBooks?authorId=' + param.author+ '&amp;offset=param.offset&amp;get=param.get',
          method: 'GET',
          resolveWithFullResponse: true
      }
  };
  
   Data: {
      Books : [],
      MoreData: true
      }
  </code></pre>
  
  <p>The call to getBooksFromWebserver is called like this:</p>
  
  <pre><code>module.exports.GetBooks = function (data) {
  
      rp(getBooksFromWebserver(data), function(err, response, body) {
          if (err) {
  
             } 
              response.setEncoding(appLibconfig.encoding);
              writer.transformAndWrite(data, body);
  
      });
  
  };
  </code></pre>
  
  <p>The issue I am having is implementing a Deferred to wait until MoreData returns false (so I know all data is retrieved) before proceeding to  writer.transformAndWrite(data, body);</p>
  
  <p>So, it needs to iterate the getBooksFromWebserver call, cache the response locally to retain each response, then when MoreData is false, write the data and and clear the cache.</p>
  
  <p>I have tried various techniques but am struggling to implement a pattern that works, any guidance greatly appreciated.</p>