Created
May 10, 2015 23:20
-
-
Save CodeMan99/271b426683ea1ef2769f to your computer and use it in GitHub Desktop.
This file contains 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 http = require('http'); | |
function http_collect(url, callback) { | |
http.get(url, function(resp) { | |
var data = []; | |
resp.setEncoding('utf8'); | |
resp.on('data', Array.prototype.push.bind(data)); | |
resp.on('end', function() { | |
return callback(null, data.join('')); | |
}); | |
}).on('error', callback); | |
} | |
module.exports = http_collect; |
This file contains 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 http_collect = require('./http_collect'); | |
main(process.argv.slice(2)); | |
function main(argv) { | |
this.results = []; | |
this.log = DeferFor(argv.length, logResults); | |
for (var i = 0; i < argv.length; i++) { | |
http_collect(argv[i], addResultAt.bind(this, i)); | |
} | |
} | |
function addResultAt(index, err, data) { | |
if (err) return this.log(err); | |
this.results[index] = data; | |
return this.log(null, this.results); | |
} | |
function logResults(err, data) { | |
if (err) throw err; | |
data.forEach(function(d) { | |
console.log(d); | |
}); | |
} | |
// functionally equivalent to "after" - https://github.com/Raynos/after | |
function DeferFor(count, callback) { | |
if (count <= 0) { | |
return callback(new Error('Count must be greater than zero')); | |
} | |
return function(err, data) { | |
if (err) return callback(err); | |
if (--count === 0) { | |
return callback(null, data); | |
} | |
if (count < 0) { | |
return callback(new Error('Used DeferFor callback more than requested')); | |
} | |
}; | |
} |
This file contains 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 after = require('after'); | |
var http_collect = require('./http_collect'); | |
main(process.argv.slice(2)); | |
function main(argv) { | |
this.results = []; | |
this.log = after(argv.length, logResults); | |
for (var i = 0; i < argv.length; i++) { | |
http_collect(argv[i], addResultAt.bind(this, i)); | |
} | |
} | |
function addResultAt(index, err, data) { | |
if (err) return this.log(err); | |
this.results[index] = data; | |
return this.log(null, this.results); | |
} | |
function logResults(err, data) { | |
if (err) throw err; | |
data.forEach(function(d) { | |
console.log(d); | |
}); | |
} |
This file contains 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 async = require('async'); | |
var http_collect = require('./http_collect'); | |
async.map(process.argv.slice(2), http_collect, logResults); | |
function logResults(err, data) { | |
if (err) throw err; | |
data.forEach(function(d) { | |
console.log(d); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment