Created
May 23, 2015 15:31
-
-
Save mfellner/bfb6af8f3aaa6074f8ca to your computer and use it in GitHub Desktop.
juggling-async.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
var http = require('http') | |
var bl = require('bl') | |
var results = [] | |
var count = 0 | |
function printResults () { | |
for (var i = 0; i < 3; i++) | |
console.log(results[i]) | |
} | |
function httpGet (index) { | |
http.get(process.argv[2 + index], function (response) { | |
response.pipe(bl(function (err, data) { | |
if (err) | |
return console.error(err) | |
results[index] = data.toString() | |
count++ | |
if (count == 3) | |
printResults() | |
})) | |
}) | |
} | |
for (var i = 0; i < 3; i++) | |
httpGet(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment