Last active
August 29, 2015 13:57
-
-
Save joshuakfarrar/9672887 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// "But for this exercise, try and do it without any external helper library." | |
// this version is my (sent1nel's) work | |
var http = require('http') | |
, events = require('events') | |
, util = require('util') | |
, bl = require('bl') | |
function Server() {}; | |
util.inherits(Server, events.EventEmitter); | |
Server.prototype.get = function (id) { | |
var self = this; | |
http.get(process.argv[id + 2], function (res) { | |
res.pipe(bl(function(err, data) { | |
if (err) return console.error(data); | |
self.emit('data', id, data.toString()); | |
})); | |
}); | |
} | |
module.exports = new Server; |
This file contains hidden or 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 server = require('./9-module') | |
var buffers = [] | |
, received = 0 | |
server.on('data', function(id, buffer) { | |
received++; | |
buffers[id] = buffer; | |
if (received == 3) this.emit('done'); | |
}); | |
server.on('done', function() { | |
for (var i = 0; i < buffers.length; i++) | |
console.log(buffers[i]); | |
}); | |
for (var i = 0; i < 3; i++) | |
server.get(i); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment