title: node.js: downloading many images from urls: timeout as too many symultanous downloads
body: <p>I have code that downloads images from given URLs. It loops through products in database and for each product calls Image download.</p>
  
  <p>However, everything seems to happen asyncrhonously and if there are too many URLs (images to download) process stops with TIMEOUT. This is logicall as there are about 3000 images to download. </p>
  
  <p>Can you give me some suggestions how to improve the code so that it downloads for instance only "10" images and do not start downloading for next images while previous "10" not finished? 
  I am not used to node.js async functionality yet.</p>
  
  <pre><code>// download file
  var download = function (uri, filename, callback) {
      request.head(uri, function (err, res, body) {
          request(uri).pipe(fs.createWriteStream(filename))
              .on('error', () =&gt; {
                  console.log('Err');
              })
              .on('close', callback);
      })
  
  };
  
  // main code - looping through products in DB and upload file for each product (about 3000 products)
  
  knex("products").select("products.id as id", "products.img as img", "products.code as code")       
      .mapSeries(function (product) {
              var imgName = 'imgs/' + product.code.toString() + ".png";
              download(product.img, imgName, function () {
                  knex("products").where("id", product.id).update("img_path", imgName).then(() =&gt; {
  
                  });
              });
          }
      })
      .then(() =&gt; {
          // console.log('done');
      });
  </code></pre>