3 ms·
If a callback calls the enclosing function, would it be called a recursive call?
I asked this question[1] on stackoverflow but could not get any convincing answer:
http.request(options, function(res) {
res.on('data', function(chunk) {
data+=chunk;
});
res.on('end', function(){
//do some stuff
http.request(options, function(res) {...});//is this recursive?
});
}).end();
Or a simpler case:
suppose there is a function which reads a file character by character:
var noOfChar = 10;
var i = 0;
readChar(function processChar(char){
if(i < noOfChar) {
console.log(char);
i++;
readChar(processChar); //is this recursive?
}
}
- anuragpeshne 12y agoSO link: http://stackoverflow.com/questions/28037475/if-a-callback-calls-the-enclosing-function-would-it-be-called-a-recursive-call http://stackoverflow.com/questions/28037475/if-a-callback-ca...
- ts-jondin 12y agoEdit: Oh, and as for the question itself, no I would not call it a recursive function just because it calls itself; I'd call it a self-referencing function. Where recursive functions are a subset of self-referencing functions. 1. Why does it matter? 2. Your first example I would simply call an asynchronous loop. 3. Your second example could possibly be called single recursion. 4. Recursion should at least consist of a base case and a recursive case, each self-reference should bring the input value closer to the base case which would end the recursion. (Your second example has a recursive case and falls out into an undefined base case). 5. An example of 4 would be a tree structure where the base case is a leaf and recursion cases are branches. function walk (value) { if (Array.isArray(value)) { value.forEach(walk); } else { console.log(value); } } walk([[[1, 2, 3, [4, 5, 6]]],[[7, [8, 9, 10], 11, [12]]],13,14]); would output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
- anuragpeshne 12y agoThanks, "I would not call it a recursive function just because it calls itself; I'd call it a self-referencing function." - this cleared a lot of things. I'll a write an answer based on this and other things that I read. This will help someone someday.