Why use asynchronous?
Because js is single-threaded. Only run on the main thread js engines. So only js code line by line execution
. The same time can not perform multiple tasks js code. This results in ajax request if there is a time-consuming calculations alive.
If you do not asynchronous, the user will have to wait a long time. Since this time the task is not complete, users of other operating
Will not respond
Common asynchronous mode
Callback
Event Listeners
promise
。。。
Js how to implement asynchronous
js event loop mechanism
js parsing time. It will be divided into two categories task engine, synchronous and asynchronous tasks Tasks
Sync task: pushed to the execution order execution stack
Asynchronous task: when it can be executed, will be put into the task queue, waiting to be executed
When performing synchronized execution stack all finished, js engine will to the task list to see if there is a mission and tasks in
The execution stack execution, after executing a task list went to check if there is an executable tasks. This loop checking mechanism is called an event loop mechanism.
Promise
Promise abstract asynchronously processed and subjected to various operations of the objects
In short, let asynchronous look good
promise is a class
Cycle in the asynchronous processing