Last active
August 29, 2016 00:00
-
-
Save dengjonathan/35805918f100ed6493a225f689b33f5d to your computer and use it in GitHub Desktop.
An example of piping in output from an HTTP request into another function
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'); | |
var bl = require('bl'); | |
// file path is from enviroment variable passed in through CLI | |
var urls = process.argv.slice(2); | |
var results = []; | |
var returned = 0; | |
function httpGet(index) { | |
http.get(urls[index], function(response) { | |
response.pipe(bl(function(err, data) { | |
if (err) { | |
console.error(err); | |
} | |
results[index] = data.toString(); | |
returned++; | |
if (returned === 3) { | |
results.forEach(function(each) { | |
console.log(each); | |
}); | |
} | |
})).on('error', function(err) { | |
console.error("There was an error submitting the http request."); | |
}); | |
}); | |
} | |
for (var i = 0; i < urls.length; i++) { | |
httpGet(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment