Skip to content

Instantly share code, notes, and snippets.

@ggreenleaf
Created July 24, 2014 16:07
Show Gist options
  • Select an option

  • Save ggreenleaf/b7620debe960459392ed to your computer and use it in GitHub Desktop.

Select an option

Save ggreenleaf/b7620debe960459392ed to your computer and use it in GitHub Desktop.
var http = require('http');
var concatStream = require("concat-stream")
var urls = process.argv.slice(2);
var results = [];
function printResults (results) {
results.forEach (function (result) {
console.log(result)
})
}
function httpGet (index) {
http.get(urls[index], function (response) {
response.setEncoding("utf8")
response.pipe(concatStream( function (data) {
results.push(data)
if (results.length == 3)
printResults(results)
}))
})
}
for (var i = 0; i < urls.length; i++) {
httpGet(i)
}
// /* Their solution */
// 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) // yay! we are the last one!
// 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