Open muthuvenkat opened 4 years ago
I personally find the for loop confusing. Perhaps this will provide you with some clarity:
'use-strict';
const http = require('http')
const url1 = process.argv[2];
const url2 = process.argv[3];
const url3 = process.argv[4];
// see notes
getContents(url1, function () {
getContents(url2, function () {
getContents(url3);
});
});
function getContents(url, callback) {
http.get(url, function (res) {
let rawData = "";
res.setEncoding('utf8');
res.on('error', function(err) {
return console.log(err)
})
res.on('data', function(data) {
rawData += data;
});
res.on('end', function() {
console.log(rawData);
callback ? callback() : null;
})
})
}
Notes: The getContents function calls are synchronous. But I think that was the purpose of the exercise.
Also the getContents example are known as a callback hell. For a later stage probably something such as async await would be more useful. But for this exercise we actually
This solution works
Where as this one doesn't work
The only difference the method getData(i) , why one works and other doesn't any ideas ?