Javascript is a single-threaded language that runs from top to bottom. As a scripting language, its great advantage is also due to single-threaded language.

When I was learning ABOUT JS and Node, the task queue was mentioned in the course, and there are two types of tasks, synchronous and asynchronous. A synchronization task refers to a task that is queued to be executed on the main thread. The next task can be executed only after the first task is completed. Asynchronous tasks are tasks that do not enter the main thread but enter the task queue. The task queue notifies the main thread that an asynchronous task is ready to execute.

There are two types of tasks, synchronous and asynchronous. A synchronization task refers to a task that is queued to be executed on the main thread. The next task can be executed only after the first task is completed. Asynchronous tasks are tasks that do not enter the main thread but enter the task queue. The task queue notifies the main thread that an asynchronous task is ready to execute.

The timer class is often used as an interview question in EventLoop

The main focus is the order in which the code is executed

The micro queue and macro queue in Node rely on the nodeJS event loop knowledge of Teacher Ruan Yifeng

(1) V8 engine parses JavaScript scripts.

(2) Parsed code, call Node API.

(3) Libuv library is responsible for the execution of Node API. It assigns different tasks to different threads, forming an Event Loop that asynchronously returns the execution results of the tasks to the V8 engine.

(4) The V8 engine returns the result to the user.

In addition to setTimeout and setInterval, Node.js provides two other “task queue” related methods: process.nextTick and setImmediate. They can help us deepen our understanding of “task queues”.

The process.nextTick method fires the callback function —- at the end of the current “execution stack” —- before the next Event Loop (the main thread reads the “task queue”). That is, the task it specifies always takes place before all asynchronous tasks. The setImmediate method adds an Event to the end of the current “task queue,” which means that the task it specifies is always executed on the next Event Loop, much like setTimeout(fn, 0).

The multiple Process. nextTick statements always run once on the current “stack”, and multiple setImmediate loops may require several times to run. In fact, that’s why Node.js 10.0 added the setImmediate method, otherwise recursive calls to Process. nextTick like the one below would go on forever and the main thread would never read the “event queue”!