Skip to content

Instantly share code, notes, and snippets.

@codeasashu
Created May 7, 2018 18:16
Show Gist options
  • Select an option

  • Save codeasashu/6d1732593318992e6c03c5565ec7f21f to your computer and use it in GitHub Desktop.

Select an option

Save codeasashu/6d1732593318992e6c03c5565ec7f21f to your computer and use it in GitHub Desktop.

This is a reminder guide for me to how and what to know about the weird things in JS. Get to basics first.

  1. What are Event loops in Javascript? Refs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop https://www.youtube.com/watch?v=8aGhZQkoFbQ

  2. What will be output of this:

console.log(1)
setTimeout(function(){ console.log(2) })
console.log(3)

A: 1,3,2.

Why? Well because setTimeout with 0 interval doesn't mean it will execute after 0 milliseconds. the setTimeout needs to wait for all the codes to complete even though you specified a particular time limit for your setTimeout Basically, this means setTimeout will wait for event Queue to be empty. Simply put, setTimeout() re-queues the new JavaScript at the end of the execution queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment