Last active
January 6, 2016 17:14
-
-
Save umairsd/721f85a8114a5f57fb0c to your computer and use it in GitHub Desktop.
Solution to Juggling Async problem (Learn You the Node.js)
This file contains 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
// 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