Js execution mechanism is single threaded, non-blocking (with eventLoop)

The event loop consists of three modules:

  • The main thread

  • Macro queues: setInterval, setTimeout, I/O, UI Rendering

  • Microqueue: promise.then

Execution order

  1. Execute the main thread first
  2. Encounter a macro task, add to the macro queue, encounter a microtask, add to the microqueue
  3. The main thread is finished
  4. Execute microqueue. Microqueue execution is complete
  5. Execute one task in the macro queue at a time
  6. Execute microqueue, complete
  7. Cycle…