Skip to content

Instantly share code, notes, and snippets.

@umairsd umairsd/program.js
Last active Jan 6, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.