Unlike synchronous mode, asynchronous mode apis do not wait for the end of a task before starting the next one. For time-consuming operations, it starts immediately and moves on to the next task. The subsequent logic for time-consuming tasks is typically defined in the form of a callback function that is automatically executed after the internal time-consuming task has completed. Asynchronous patterns are important to Javascript because without them the Javascript language cannot handle a large number of time-consuming operations simultaneously. For developers, the biggest difficulty with asynchrony under single-threaded mode is the out-of-order code execution.

Asynchronous call in JS implementation process, as well as its basic principle: first load the whole code into the execution environment, and in the call stack pressed into an anonymous function call, and then execute each line of code in turn. When encountering synchronous code, it will carry out the operation process of pushing-execution-stack; Asynchronous code is also pushed, and then a countdown timer is started in the internal API for this asynchronous operation. This timer works independently and is not affected by the current JS thread. When the countdown is turned on, the call is complete for the asynchronous program. It also pops out of the execution stack and performs subsequent tasks. After all tasks in the call stack are completed, the Event Loop will be started to work. The Event Loop does only one thing: listen to the Call Stack, the Call Stack and the message Queue. Once all tasks in the call stack have finished, the event loop pulls the first callback function from the message queue and pushes it onto the call stack. As we execute the call stack, the countdown timer executes at the same time and enters the message queue after the countdown ends. As soon as the message queue changes, the event loop listens. Tasks in the message queue are then pushed back to the call stack for execution. For the call stack, a new round of calls is started, and the execution process is the same as before. The same is true if the procedure encounters an asynchronous call. It will first be put into the API environment to execute separately, and then follow the process. Until there are no more tasks to execute in our call stack or message queue, the whole code ends. If our call stack is a worksheet in progress, the message queue is a worksheet to do. And the JS execution engine is a call stack to finish all the tasks, and then through the event loop from the message queue to take a task to continue to execute, and so on the whole process, we can at any time to the message queue to put some tasks. These tasks are in the message queue and queue for the event loop.

In JS, the JS thread makes an asynchronous call at some point, and then it goes on to perform another task, at which point the asynchronous thread performs the asynchronous task alone. Then, after the task is executed, the callback for that task is put into the message queue. After completing all the tasks, the main JS thread will call and execute the tasks in our message queue.

Summary: Synchronous or asynchronous, not the way we write code, but the apis provided by our runtime environment work in synchronous or asynchronous mode. For synchronous apis, the characteristic is that the code doesn’t continue until the task is finished. For asynchronous apis, it just tells you to start the task, and then it continues, so the code doesn’t wait on this line for the task to finish.