Skip to content

Instantly share code, notes, and snippets.

@weisk
Created August 12, 2016 17:57
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 weisk/fd962c49eafa3b80c1361cf6b818b745 to your computer and use it in GitHub Desktop.
Save weisk/fd962c49eafa3b80c1361cf6b818b745 to your computer and use it in GitHub Desktop.
learnyounode 13 async
var http = require('http');
var requests = process.argv.slice(2);
var results = [];
var count = 0;
for (var i = 0, len = requests.length; i < len; i++) {
getRequest(requests[i], i);
}
function getRequest(url, pos) {
http.get(url, function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
results[pos] = body;
count++;
if (count == len) {
console.log(results.join('\n'));
}
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment