title: Wait until the rest api returns result and then call api again
body: <p>Is there any other way other than <code>setTimeout()</code> to wait for the result come back after api call and then call that api again immediately after result is received?</p>
  
  <p>In my case, I have to call <code>n</code> times times (depending on user input) and the api call takes 20-40 seconds to process and return result from the backend. If I send another request, while the previous one is still being processed in the backed, then it breaks the backend.</p>
  
  <p>What is the right way to do call api once result is received and fully processed?</p>
  
  <pre><code>sendRequest() {
      let self = this;
          function f() {
              if (self.state.callPerSite &lt; self.state.selectedSites.length) {
                  self.processData(transid);
                     setTimeout(f, 20)
                  }
              } else {
                  self.setState({progress: 100});
              }
          }
          f();
  } 
  
  processData(transid) {
          rest.apiCall({transactionId: transid}, 'json').then(results =&gt; {
  
              if (!firstCallIsProcessed) {
                  firstCallIsProcessed = true;
              }
              callPerSite++;
  
              this.setState({
                  resultsData: results.data,
                  callPerSite: callPerSite
              });
  
          }).catch(err =&gt; this.onError(err));
  </code></pre>