Skip to content

Instantly share code, notes, and snippets.

@voidnerd
Created December 7, 2017 12:21
Show Gist options
  • Save voidnerd/2f9bf3ff679e0de4024bf80aaef4338a to your computer and use it in GitHub Desktop.
Save voidnerd/2f9bf3ff679e0de4024bf80aaef4338a to your computer and use it in GitHub Desktop.
// JOGGLING ASYNC
var http = require('http');
var urls = [process.argv[2], process.argv[3], process.argv[4]];
var count = 0;
var arr = [];
function get(a) {
http.get(urls[a], (res) => {
var word = ''; // holder of strings
res.setEncoding('utf8');
res.on('data', (data) => {
word += data; // add each data to holder of strings
});
res.on('error', (error) => {
console.log(error) //if error then log error
});
res.on('end', () => {
count++; // when stream is done increment count
arr[a] = word;
if(count === 3) { // if count is equal 3 that means process is complete for three urls
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]); // log array
}
}
})
});
}
for (let index = 0; index < 3; index++) {
get(index); // call function to display string orderly
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment