title: NodeJS using request inside a loop
body: <p>I use NodeJS and request lib to make some request to an API.</p>
  
  <p>I understand now that all requests are async and so it doesn't "wait" the result of the GET call, so the index of my Loop is always the same.</p>
  
  <p>I was wondering if there was any simple way (without any lib) to wait for the response of the request call ?</p>
  
  <p>For now, my code is this :</p>
  
  <pre><code>for (var i in entries) {    
      var entryId = entries[i]['id'];
  
      var options = {
          url: 'https://api.com/'+ entryId +'/get/status',
          method: 'GET',
          headers: {
              'Authorization': auth
          }
      };
  
      console.log(' ENTRY ID &gt; '+ entryId);
  
      request(options, function(error, response, body) {          
          var response = JSON.parse(body);
  
          if (response.status.code == 200) {
  
              var id = response.status.id;
              var data = [];
              data['id'] = id;
  
              data = JSON.stringify(data);
  
              // show first entryId of loop  
              console.log(' &gt; MY ID : '+ id + ' - '+ entryId);
  
              options = {
                  host: hostname,
                  port: 80,
                  path: '/path/function2',
                  method: 'PUT'
              };
  
              var post = http.request(options, function(json) { 
                  var body = '';
  
                  json.on('data', function(d) {
                      body += d;
                  });
                  json.on('end', function() {
                      console.log('&gt; DONE');
                  });
  
              }).on('error', function(e) {
                  console.log(e);
              });
  
              post.write(data);
              post.end(); 
          }
      }); 
  }
  </code></pre>