2
  1. Code which doesn't cause stack overflow
const s = async () => {
      await new Promise((resolve) => {
        setTimeout(resolve, 1);
      });

      await s();
      console.log("exit");
};

    s();
  1. Code causes stack overflow
const s = async () => {
      await s();
      console.log("exit");
 };

    s();

Question:

case 2 causing stack overflow is what i have expected, what i don't understand is what why case 1 won't. Please help me with this.

Hanwen D.
  • 31
  • 1
  • 1
  • 2
    [Understanding Event Queue and Call stack in javascript](https://stackoverflow.com/q/39459236) – VLAZ Jul 15 '22 at 06:01
  • 1
    Oh I didn't expect we have duplicate targets for this, but I did find some good ones. Interestingly some ask "*Why does a recursive async function overflow?*" while others ask "*Why does a recursive async function not overflow?*" - it depends on the structure of the code and where the `await` is placed :-) – Bergi Jul 15 '22 at 08:52
  • 1
    `async function s() { await undefined; s(); }` is all that is required for not causing a stack overflow. – Bergi Jul 15 '22 at 08:53

0 Answers0