title: NODE - wait until one api call has completed before triggering the next api call
body: <p>I am making a POST request in one of my routes add-users. I have created an array called success. Once the request has returned the response I want to trigger the next API call. </p>
  
  <p>It is not working at the moment as I think I am sending too may API requests at once. I think the solution is that I wait until the first response has returned a response and finished, and then trigger the next API call.</p>
  
  <p>Is this assumption correct? If so, can anyone advise how to implement this? I have tried to use <code>.on('end'....</code></p>
  
  <p>Please see my code below.</p>
  
  <pre><code>app.get('/add-users', function (req, res) {
  
      var success = [];
      var count = 0;
      for(var i = 0; i &lt; users.length; i++) {
          var name = users[i].userId;
          request({
              url: url,
              headers: {
                  'Authorization': 'Bearer ' + users[i].accessToken,
                  'Content-Type': 'application/json'
              },
              method: 'PUT',
              json: true
          }, function(err, resp, body){
              if (!err &amp;&amp; resp.statusCode === 200) {
                  success.push(name);
              }
          })
          .on('end', function(){
              console.log('this is the end');
              count++;
              if(count === users.length) {
                  res.json(success);
              }
          });
      }
  });
  </code></pre>