Skip to content

Instantly share code, notes, and snippets.

@tankala
Created July 1, 2018 10:01
Show Gist options
  • Save tankala/ba125c7b6e69ed86994df707a3723ec2 to your computer and use it in GitHub Desktop.
Save tankala/ba125c7b6e69ed86994df707a3723ec2 to your computer and use it in GitHub Desktop.
Downloading 2 avatars in parallel at a time using Async in Node.js
const request = require('request');
const fs = require('fs');
const async = require('async');
const avatarsNamesArray = ['1.png','2.png','3.png','4.png','5.png','6.png','7.png','8.png','9.png','10.png'];
getAndStoreAvatar = function(avatarName, callback) {
console.log(avatarName + ' is downloading');
let stream = request
.get('https://api.adorable.io/avatars/285/' + avatarName)
.on('error', function(err) {
console.log(err);
callback();
})
.pipe(fs.createWriteStream(avatarName));
stream.on('finish', callback);
};
async.forEachLimit(avatarsNamesArray, 2, getAndStoreAvatar, function() {
console.log('Avatars download complete');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment