The definition of recursion says that it is a direct or indirect function call from itself.
T. o. The go function in the following example is recursive:
function pass(f, i) { return f(i); } function go(i) { if (i < 3) { console.log(i); pass(go, i + 1); } else { console.log(new Error().stack); } } go(0); html .as-console-wrapper { top: 0; max-height: none; } Now let's replace the pass function with an asynchronous operation:
function go(i) { if (i < 3) { console.log(i); setTimeout(go, 0, i + 1); } else { console.log(new Error().stack); } } go(0); html .as-console-wrapper { top: 0; max-height: none; } What changed? In the first case, the call stack contained a chain go-pass-go-pass-go-pass-go , and in the second there was only one function - go . An asynchronous operation is placed in the operation queue and, when called, is not nested in what it planned.
Is it true to call such a call recursive?
I really want to say no, but I remember tail recursion, which also turns into a loop by the compiler. It seems like the nesting of the call stack for recursion is optional. Another point is that the calling function is not in the process of execution at the moment of the execution of the nested call, but even here the tail recursion calls into question the necessity of this fact ...