Skip to content

Commit

Permalink
wrote the solution for first problem
Browse files Browse the repository at this point in the history
  • Loading branch information
bulkan committed Dec 9, 2013
1 parent e977210 commit 68ed795
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
4 changes: 2 additions & 2 deletions problems/waterfall/problem.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ property then it does another GET request.
});

}).on('error'), function(e) {
cb(err);
});
cb(err);
});
}
], function(err, result){
if (err) return console.error(err);
Expand Down
24 changes: 5 additions & 19 deletions problems/waterfall/setup.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
var http = require('http');

function server (i, delay, port) {
return http.createServer(function (req, res) {
writeWords(i, delay, res)
}).listen(port)
}

module.exports = function () {
var servers = [
server(0, 100, 9345)
, server(1, 0, 9346)
, server(2, 50, 9347)
]
var server = http.createServer(function (req, res) {
res.end('boom!')
}).listen(9345)

return {
args : [
'http://localhost:9345'
, 'http://localhost:9346'
, 'http://localhost:9347'
__dirname + '/url.txt'
]
, stdin : null
, long : true
, close : function () {
servers.forEach(function (server) {
server.close()
})
}
, close : server.close.bind(server)
}
}
30 changes: 30 additions & 0 deletions problems/waterfall/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var fs = require('fs')
, http = require('http')
, async = require('async');

async.waterfall([
function(done){
fs.readFile(process.argv[2], function(err, data){
if (err) return done(err);
done(null, data)
});
},

function(data, done){
var body = '';
http.get(data.toString().trimRight(), function(res){
res.on('data', function(chunk){
body += chunk.toString();
});

res.on('end', function(chunk){
done(null, body);
});
}).on('error', function(e){
done(e);
});
}
], function(err, result){
if (err) return console.error(err);
console.log(result);
});
1 change: 1 addition & 0 deletions problems/waterfall/url.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost:9345

0 comments on commit 68ed795

Please sign in to comment.