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
- Execute the main thread first
- Encounter a macro task, add to the macro queue, encounter a microtask, add to the microqueue
- The main thread is finished
- Execute microqueue. Microqueue execution is complete
- Execute one task in the macro queue at a time
- Execute microqueue, complete
- Cycle…