Task queue and event loop (event loop)

1. All synchronization tasks are executed on the main thread, forming an execution stack.

2. In addition to the main thread, there is also a task queue. As long as the asynchronous task has a running result, a time stamp is implanted in the task queue.

3. After the main thread completes all tasks (the execution stack is cleared), it will read the task queue, execute the micro task queue first, and then execute the macro task queue.

4. Repeat the above three steps.

As long as the main thread is empty, the task queue will be read. This is the operating mechanism of js, also known as event loop.

Execution order

1. The execution order of macro tasks and micro tasks on the
main thread Execution order: main thread >> micro tasks created on the main thread >> macro tasks created on the main thread

2. The macro task contains micro tasks.
Execution order: main thread >> macro task queue 1 on the main thread >> micro tasks created in macro task queue 1

Guess you like

Origin blog.csdn.net/qq_45846359/article/details/109434356