title: Recursively calling an asynchronous API call
body: <p>I'm trying to fetch data from an API that only returns 1000 items per call, and I want to do this recursively until I have all the data. </p>
  
  <p>I don't know beforehand how many items there are in total, so after every call I have to check </p>
  
  <p>If the call was synchronous, I'd use something like this:</p>
  
  <pre><code>function fetch(all, start) {
      const newData = getData(start, 1000);
      all = all.concat(newData);
      return (newData.length === 1000) ? fetch(all, all.length) : all;
  }
  </code></pre>
  
  <p>However, the <code>getData()</code> call here is asynchronous. Using a <code>Promise.all()</code> doesn't work because I don't know beforehand how many calls I need so I can't prepare an array of calls.</p>
  
  <p>I have the feeling I could solve this with generators or with <code>async</code>/<code>await</code> but I don't know how. Can anyone point me in the right direction?</p>
  
  <p>In case it makes a difference, I'm using Angular 4.</p>