Skip to content

Commit

Permalink
first problem description
Browse files Browse the repository at this point in the history
  • Loading branch information
Bulkan Evcimen committed Dec 9, 2013
1 parent cef13e2 commit 0d93ca9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions problems/hello_world/problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
In Node.js and browsers there is three ways to do {bold}asynchronous{/bold} JavaScript.

The first way leads to what we call {italic}Callback Hell{/italic}. Callback Hell
can be minimized by following the tips at http://callbackhell.com.

Another method is to use a Promise package. Using promises will simplify your
code but it also adds another layer of abstraction.

The last method is by using the async package by Caolan McMahon. With {italic}async{/italic}
we are still writing callbacks but without falling into the callback hell or
adding another layer of abstraction with promises.

More often than not you will need to do multiple asynchronous calls one after
the other with each call dependant on the result of previous asynchronous call.
We can do this with the help of async.waterfall.

For example the following code will do a GET request to http://localhost:3131
in the first waterfall function. The response body is pased as an argument to
the next waterfall function via the callback. The second function in the waterfall
accepts the body as a parameter and JSON.parse's it to get to the port
property then it does another GET request.

var http = require('http'),
, async = require('async');

async.waterfall([
function(cb){
var body = '';

// response is JSON encoded object like the following {port: 3132}
http.get("http://localhost:3131", function(res){

res.on('data', function(chunk){
body += chunk.toString();
});

res.on('end', function(){
cb(null, body);
});
}).on('error'), function(e) {
cb(err);
});
},

function(body, cb){
var port = JSON.parse(body).port;

var body = '';

http.get("http://localhost:" + port, function(res){
res.on('data', function(chunk){
body += chunk.toString();
});

res.on('end', function(){
cb(null, body);
});

}).on('error'), function(e) {
cb(err);
});
}
], function(err, result){
if (err) return console.error(err);
console.log(result);
});



In this problem you will need to write a program that first reads the contents of a file.
The path will be provided as the first command-line argument to your program.
The file will contain a single URL. Using `http.get` create GET request to to this url and
console.log the response body.

0 comments on commit 0d93ca9

Please sign in to comment.