Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cmawhorter
Created July 10, 2014 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmawhorter/31ab6aa59990e8e82922 to your computer and use it in GitHub Desktop.
Save cmawhorter/31ab6aa59990e8e82922 to your computer and use it in GitHub Desktop.
Downloads all files in an array 2 at a time
var fs = require('fs');
var path = require('path');
var async = require('async');
var request = require('request');
var tasks = {};
var json = { /* your records */ };
json.forEach(function(record) {
tasks[record.imagefile] = function(callback) {
request({
url: 'http://example.com/Clipart/'+record.imagefile,
encoding: null
}, function (err, res, data) {
if (err) return callback(err);
if (!err && res.statusCode == 200) {
var f = path.join(process.cwd(), 'downloads/' + record.imagefile);
fs.writeFile(f, data, function(err) {
if (err) return callback(err);
callback(null, f);
});
}
});
};
});
async.parallelLimit(tasks, 2, function() {
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment