can anyone tell why my solution doesn't work? while its some what similar to official solution ..
var http = require('http');
var bl = require('bl');
let count = 0;
const results = [];
for (let i = 0; i < 3; i++) { // what makes the difference by adding a function here, instead of for loop?
http.get(process.argv[i + 2], function (res) {
res.setEncoding('utf8');
res.pipe(bl(function (err, data) {
if (err){
return console.error(err);
}
results[i] = data.toString();
count ++;
if (count == 3) {
printResults()
}
}));
});
}
function printResults() {
for (let i = 0; i < 3; i++) {
console.log(results[i]);
}
}
can anyone tell why my solution doesn't work? while its some what similar to official solution .. var http = require('http'); var bl = require('bl'); let count = 0; const results = [];
for (let i = 0; i < 3; i++) { // what makes the difference by adding a function here, instead of for loop? http.get(process.argv[i + 2], function (res) { res.setEncoding('utf8'); res.pipe(bl(function (err, data) { if (err){ return console.error(err); } results[i] = data.toString(); count ++; if (count == 3) { printResults() } })); });
}
function printResults() { for (let i = 0; i < 3; i++) { console.log(results[i]); } }
Originally posted by @PRANAY100 in https://github.com/workshopper/learnyounode/issues/625#issuecomment-468150927