I was thinking what happens when an async function recursively calls itself infinitely. My thought was that it will not cause stack overflow. But I can't exactly point out why this is the case.
const foo = async () => {
const txt = await Promise.resolve("foo");
console.log(txt);
foo();
}
foo();
The code above prints "foo" infinitely without overflowing the stack.
My idea is that the code is conceptually similar to the following, it doesn't cause stack overflow because the recursive call to foo()
is inside the callback, the original call to foo()
will return before that.
const bar = () => {
console.log("foo");
foo();
}
const foo = () => {
setImmediate(bar);
}
foo();
I am looking for the exact answer as to what happens in the case of the async function.