These are chat archives for ninjascribble/coolkidz

9th
Jan 2015
Daniel Harrie
@dharrie
Jan 09 2015 21:10
Let's see, so...
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)
Just for kicks, here's my version (I like it better, mostly)
var http = require('http');
var bl = require('bl');
var urls = process.argv.splice(2, 5);
var results = [];

function checkCompletness() {
    var done = urls.every(function(url, index) {
        return !!results[index];
    });

    if (done) {
        results.forEach(function(result) {
            console.log(result);
        });
    }
}

urls.forEach(function(url, index) {
    http.get(url, function(response) {
        response.pipe(bl(function (err, data) {
            if (err)
                return console.error(err);
            results[index] = data.toString();            
            checkCompletness();
        }));
    });
});
Yay, I broke it! ;)
This message was deleted
Daniel Harrie
@dharrie
Jan 09 2015 21:15
Neat, it let me edit it until it worked. Leading spaces are important.