Skip to content

Instantly share code, notes, and snippets.

@umairsd
Last active January 6, 2016 17:14
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 umairsd/721f85a8114a5f57fb0c to your computer and use it in GitHub Desktop.
Save umairsd/721f85a8114a5f57fb0c to your computer and use it in GitHub Desktop.
Solution to Juggling Async problem (Learn You the Node.js)
// Note: Does not use any external node.js packages.
// *** Exercise 9: JUGGLING ASYNC
var http = require('http');
var count = 3;
var urls = process.argv.slice(2);
var results = [];
function httpGet(index)
{
results[index] = "";
http.get(process.argv[index+2], function (response) {
response.setEncoding("utf8");
response.on("data", function(data) {
results[index] = results[index] + data;
});
response.on("end", function() {
count = count - 1;
if (count <= 0) {
printResults();
}
});
});
}
for (var i = 0; i < 3; i++) {
httpGet(i);
}
function printResults() {
results.forEach(function(r) {
console.log(r);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment