- Calling foo() will put the foo function into the call stack.
- While processing the code inside, JS engine encounters the setTimeout.
- It then hands over the foo callback to the WebAPIs (arrow 1) and returns from the function. The call stack is empty again.
- The timer is set to 0, so the foo will be sent to the Task Queue (arrow 2).
- As, our call stack was empty, the event loop will pick the foo callback and push it to the call stack for processing.
- The Process repeats again and stack doesn't overflow ever.