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.

3
  • 2
    Understanding Event Queue and Call stack in javascript Commented Jul 15, 2022 at 6: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 :-) Commented Jul 15, 2022 at 8:52
  • 1
    async function s() { await undefined; s(); } is all that is required for not causing a stack overflow. Commented Jul 15, 2022 at 8:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.