-
Synchronous and asynchronous tasks go to different places of execution. Synchronous tasks go to the main thread, and asynchronous tasks go to the Event Table and register functions for later Event loops to call.
-
When the specified Event completes (important), the Event Table moves this function to the Event Queue
-
If the tasks in the main thread are empty after execution, the Event Queue will read the corresponding function and enter the main thread for execution
-
This process is repeated over and over again, often referred to as an Event Loop.
-
When a function is passed to process.nexttick (), it instructs the engine to call the function at the end of the current operation (before the next event loop tick begins).
-
So the schemes to achieve asynchrony are:
-
Callback function call.
-
Event listening.
-
We assume that there is a “signal center” that “publishes” a signal when a task completes, and that other tasks can “subscribe” to the signal center to know when they can start executing. This is called a publish-subscribe pattern, also known as an observer pattern.
-
Promise/A +.
-
Generator Generators/ yield.
-
Async/await.
-
The execution of the main thread is a tick, and all asynchronous results are scheduled by the “task queue”.
-
In the browser environment, common Macro tasks include setTimeout, MessageChannel, postMessage, and setImmediate. Common micro Tasks are MutationObsever and Promise.then.
-
Data changes to DOM changes are asynchronous.
-
** This is what happens during development, such as when we get data from the server interface and the data is modified. If some of our methods rely on DOM changes after data modification, we have to execute them after nextTick. 支那
-
Through the analysis of nextTick in this section, combined with setter analysis, we learned that the change of data to DOM re-rendering is an asynchronous process that occurs in the nextTick.