title: Best way to calling API inside for loop using Promises
body: <p>I have 500 millions of object in which each has n number of contacts as like below</p>
  
  <pre><code>var groupsArray = [
                      {'G1': ['C1','C2','C3'....]},
                      {'G2': ['D1','D2','D3'....]}
                       ...
                      {'G2000': ['D2001','D2002','D2003'....]}
                       ...
                  ]
  </code></pre>
  
  <p>I have two way of implementation in nodejs which is based on regular promises and another one using bluebird as shown below</p>
  
  <p>Regular promises</p>
  
  <pre><code>...
  var groupsArray = [
                      {'G1': ['C1','C2','C3']},
                      {'G2': ['D1','D2','D3']}
                  ]
  
  function ajax(url) {
    return new Promise(function(resolve, reject) {
          request.get(url,{json: true}, function(error, data) {
              if (error) {
                  reject(error);
              } else {
                  resolve(data);  
              }
          });
      });
  }
  _.each(groupsArray,function(groupData){
      _.each(groupData,function(contactlists,groupIndex){
          // console.log(groupIndex)
          _.each(contactlists,function(contactData){
              ajax('http://localhost:3001/api/getcontactdata/'+groupIndex+'/'+contactData).then(function(result) {
                  console.log(result.body);
                // Code depending on result
              }).catch(function() {
                // An error occurred
              });
          })
      })
  })
  ...
  </code></pre>
  
  <p>Using bluebird way i have used concurrency to check how to control the queue of promises </p>
  
  <pre><code>...
  _.each(groupsArray,function(groupData){
      _.each(groupData,function(contactlists,groupIndex){
          var contacts = [];
          // console.log(groupIndex)
          _.each(contactlists,function(contactData){
              contacts.push({
                  contact_name: 'Contact ' + contactData
              });
          })
          groups.push({
              task_name: 'Group ' + groupIndex,
              contacts: contacts
          });
      })
  })
  
  Promise.each(groups, group =&gt; 
      Promise.map(group.contacts,
           contact =&gt; new Promise((resolve, reject) =&gt; {
                  /*setTimeout(() =&gt; 
                      resolve(group.task_name + ' ' + contact.contact_name), 1000);*/
                  request.get('http://localhost:3001/api/getcontactdata/'+group.task_name+'/'+contact.contact_name,{json: true}, function(error, data) {
                      if (error) {
                          reject(error);
                      } else {
                          resolve(data);  
                      }
                  });
  }).then(log =&gt; console.log(log.body)), 
  {
      concurrency: 50
  }).then(() =&gt; console.log())).then(() =&gt; {
      console.log('All Done!!');
  });
  ...
  </code></pre>
  
  <p>I want to know when dealing with 100 millions of api call inside loop using promises. please advise the best way to call API asynchronously and deal the response later.</p>