Created
July 1, 2018 10:01
-
-
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
This file contains hidden or 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
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