I'm learning about callbacks and I wrote this code:
var http = require('http');
var str = "";
var count = 2;
function jugglingAsync(callback){
http.get(process.argv[count], function(response){
response.on("data", function(data){
str+=data.toString();
})
response.on("end", function(){
console.log(str);
str = "";
count++;
if(count<5) callback();
})
})
}
jugglingAsync(jugglingAsync);
It should collect 3 URL adresses given as command-line arguments. Problem is - when first callback works fine and print good result, the second gives me an error:
if(count<5) callback();
^
TypeError: callback is not a function
at IncomingMessage.<anonymous> (/home/dzikichrzan/Gdrive/Programowanie/JavaScript/learnyounode/jugglingAsync.js:13:16)
at emitNone (events.js:73:20)
at IncomingMessage.emit (events.js:167:7)
at endReadableNT (_stream_readable.js:906:12)
at nextTickCallbackWith2Args (node.js:455:9)
at process._tickCallback (node.js:369:17)
Why this function works fine only one time?