Skip to content

Instantly share code, notes, and snippets.

@gt50
Last active August 28, 2015 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gt50/1fecbdcfe1d2b45d5469 to your computer and use it in GitHub Desktop.
Save gt50/1fecbdcfe1d2b45d5469 to your computer and use it in GitHub Desktop.
How to get async calls in order?
var http = require('http');
var urls = process.argv.slice(2);
var responses = process.argv.slice(2);
var completedReqests = 0;
for (var i=0; i < urls.length;i++){
http.get(urls[i],(function (i){
return function(response){
var webData = '';
response.setEncoding('utf8');
response.on('data', function(data){
webData = webData + data;
});
response.on('error',console.error);
response.on('end',function(){
responses[i] =webData;
completedReqests++;
if (completedReqests === urls.length){
console.log(responses.join('\n'));
}
});
};
})(i));
}
//http://stackoverflow.com/questions/25825497/request-and-response-mismatch-in-async-callback
//http://blog.miguelgrinberg.com/post/easy-web-scraping-with-nodejs
// for (pool in pools) {
// var url = 'http://www.thprd.org/schedules/schedule.cfm?cs_id=' + pools[pool];
// request(url, ( function(pool) {
// return function(err, resp, body) {
// if (err) throw err;
// $ = cheerio.load(body);
// console.log(pool);
// // TODO: scraping goes here!
// }
// } )(pool));
// }
// ✓ Submission results match expected
// # PASS
// Your solution to JUGGLING ASYNC passed!
// Here's the official solution in case you want to compare notes:
// ────────────────────────────────────────────────────────────────────────────────
// 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)
// printResults()
// }))
// })
// }
// for (var i = 0; i < 3; i++)
// httpGet(i)
// ────────────────────────────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment