

If a callback calls the enclosing function, would it be called a recursive call? - anuragpeshne

I asked this question[1] on stackoverflow but could not get any convincing answer:<p>http.request(options, function(res) {
  res.on(&#x27;data&#x27;, function(chunk) {
    data+=chunk;
  });<p><pre><code>  res.on(&#x27;end&#x27;, function(){
    &#x2F;&#x2F;do some stuff
    http.request(options, function(res) {...});&#x2F;&#x2F;is this recursive?
  });</code></pre>
}).end();<p>Or a simpler case:
suppose there is a function which reads a file character by character:<p>var noOfChar = 10;
var i = 0;
readChar(function processChar(char){
  if(i &lt; noOfChar) {
    console.log(char);
    i++;
    readChar(processChar); &#x2F;&#x2F;is this recursive?
  }
}
======
ts-jondin
Edit: 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
Thanks, "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.

------
anuragpeshne
SO link: [http://stackoverflow.com/questions/28037475/if-a-callback-
ca...](http://stackoverflow.com/questions/28037475/if-a-callback-calls-the-
enclosing-function-would-it-be-called-a-recursive-call)

