title: Node.js For Loop async request
body: <p>I have for loop with a request (and another request in callback).</p>
  
  <p>I have problem with memory usage (when i am go through a lot of request at same time)</p>
  
  <p>here is the sample code : </p>
  
  <pre><code>var request = require('request');
  
  for(var j=1;j&lt;=10;j++){
  
              request({
                method: 'GET',
                url: 'https://api.domain.com/items/?page='+j+'&amp;limit=1000',
                headers: {
                  'Content-Type': 'application/json'
                }}, function (error, response, body) {
  
                var data = JSON.parse(body)
  
  
                for(var i=0;i&lt;data.length;i++){
  
                      request({
                        method: 'GET',
                        url: 'https://api.domain.com/itemDetail/'+data[i].id,
                        headers: {
                          'Content-Type': 'application/json',
                        }}, function (error, response, body) {
  
  
                          var itemDetails = JSON.parse(body);
  
                          // save items to mongodb          
  
                          }
  
                }
      });
  
  
  }
  </code></pre>
  
  <p>The solution suggested to me it was using <a href="https://github.com/caolan/async" rel="nofollow">async</a> module.</p>
  
  <p>After reading documentation i find out <code>eachlimit</code> suit my needs.</p>
  
  <p>The problem i have its i can use eachlimit for second loop but for first loop i dont know how use it (because first loop in not an array).</p>
  
  <p>here code so far i get , and its not working:</p>
  
  <pre><code>var request = require('request');
  var mongodb = require('mongodb');
  var async = require('async');
  
  var MongoClient = mongodb.MongoClient;
  
  var url = 'mongodb://localhost:27017/subtitle';
  
  for(var j=1;j&lt;=10;j++){
  
          request({
                method: 'GET',
                url: 'https://api-v2.domain.com/news/popular?page='+j+'&amp;limit=1000',
                headers: {
                  'Content-Type': 'application/json',
                  'api-version': '2',
                  'api-key': 'my-api-key'
                }}, function (error, response, body) {
  
                      try {
                          var data = JSON.parse(body);
                          } catch (e) {
                            // An error has occured, handle it, by e.g. logging it
                                console.log(e);
                          }
  
                              async.each(data, function(item, callback) {
  
                                      request({
                                        method: 'GET',
                                        url: 'https://api-v2.domain.com/news/'+item.id,
                                        headers: {
                                              'Content-Type': 'application/json',
                                              'api-version': '2',
                                              'api-key': 'my-api-key'
                                            }}, function (error, response, body) {
                                              // sava item in mongodb
                                        })
  
  
  
                                  callback();
  
                              }, function(err){
  
                                  if( err ) {
                                    console.log('A item failed to process');
                                  } else {
                                    console.log('All items have been saved successfully');
                                  }
                              });
  
  
                })
  
  
  }
  </code></pre>