Last active
August 29, 2015 14:01
-
-
Save hourback/cd272ef03c3c18e38e78 to your computer and use it in GitHub Desktop.
From the JUGGLING ASYNC lesson on http://nodeschool.io/#learn-you-node
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 concat = require('concat-stream'); | |
var http = require('http'); | |
var urls = []; | |
var results = []; | |
urls.push(process.argv[2]); | |
urls.push(process.argv[3]); | |
urls.push(process.argv[4]); | |
for (var i=0; i < urls.length; i++) { | |
httpGet(i, urls, results); | |
} | |
function httpGet (index, u, r) { | |
http.get(u[index], function (response) { | |
response.setEncoding("utf8"); | |
response.pipe(concat(function(data) { | |
r[index] = data.toString(); | |
console.log("Wrote to results[" + index + "]."); | |
console.log("Length of results is " + r.length); | |
})); | |
}); | |
} |
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
anab@anab-desktop:~/node.js/learnyounode$ node jugglingasync.js 'http://www.yahoo.com/' 'http://www.msn.com/' 'http://www.cnn.com/' | |
Wrote to results[0]. | |
Length of results is 1 | |
Wrote to results[2]. | |
Length of results is 3 | |
Wrote to results[1]. | |
Length of results is 3 | |
anab@anab-desktop:~/node.js/learnyounode$ node jugglingasync.js 'http://www.yahoo.com/' 'http://www.msn.com/' 'http://www.cnn.com/' | |
Wrote to results[0]. | |
Length of results is 1 | |
Wrote to results[2]. | |
Length of results is 3 | |
Wrote to results[1]. | |
Length of results is 3 | |
anab@anab-desktop:~/node.js/learnyounode$ node jugglingasync.js 'http://www.yahoo.com/' 'http://www.msn.com/' 'http://www.cnn.com/' | |
Wrote to results[0]. | |
Length of results is 1 | |
Wrote to results[2]. | |
Length of results is 3 | |
Wrote to results[1]. | |
Length of results is 3 | |
anab@anab-desktop:~/node.js/learnyounode$ node jugglingasync.js 'http://www.yahoo.com/' 'http://www.msn.com/' 'http://www.cnn.com/' | |
Wrote to results[0]. | |
Length of results is 1 | |
Wrote to results[2]. | |
Length of results is 3 | |
Wrote to results[1]. | |
Length of results is 3 |
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
LEARN YOU THE NODE.JS FOR MUCH WIN! | |
───────────────────────────────────── | |
JUGGLING ASYNC | |
Exercise 9 of 13 | |
This problem is the same as the previous problem (HTTP COLLECT) in that you need to use http.get(). However, this time you will be provided with three URLs as the first three command-line arguments. | |
You must collect the complete content provided to you by each of the URLs and print it to the console (stdout). You don't need to print out the length, just the data as a String; one line per URL. The catch is that you must print them out in the same order as the URLs are provided to you as command-line arguments. | |
------------------------------------------------------------------------------- | |
## HINTS | |
Don't expect these three servers to play nicely! They are not going to give you complete responses in the order you hope, so you can't naively just print the output as you get it because they will be out of order. | |
You will need to queue the results and keep track of how many of the URLs have returned their entire contents. Only once you have them all, you can print the data to the console. | |
Counting callbacks is one of the fundamental ways of managing async in Node. Rather than doing it yourself, you may find it more convenient to rely on a third-party library such as [async](http://npm.im/async) or [after](http://npm.im/after). But for this exercise, try and do it without any external helper library. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment