Created
July 10, 2014 18:32
-
-
Save cmawhorter/31ab6aa59990e8e82922 to your computer and use it in GitHub Desktop.
Downloads all files in an array 2 at a time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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