Skip to content

Instantly share code, notes, and snippets.

@singhArmani
Last active March 21, 2019 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singhArmani/b4663cc3bc8d3200bab38cc67879a074 to your computer and use it in GitHub Desktop.
Save singhArmani/b4663cc3bc8d3200bab38cc67879a074 to your computer and use it in GitHub Desktop.

Steps

  1. Calling foo() will put the foo function into the call stack.
  2. While processing the code inside, JS engine encounters the setTimeout.
  3. It then hands over the foo callback to the WebAPIs (arrow 1) and returns from the function. The call stack is empty again.
  4. The timer is set to 0, so the foo will be sent to the Task Queue (arrow 2).
  5. As, our call stack was empty, the event loop will pick the foo callback and push it to the call stack for processing.
  6. The Process repeats again and stack doesn't overflow ever.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment