Damon Road es6 of learning - js understood schematic asynchronous / synchronous (day 3)

The concept of synchronous and asynchronous how many have actually been somewhat vague, ready to proceed to the next learning Generator function, which involves the functions Promise, Promise but also involves the question of synchronous and asynchronous, synchronous and first decided to roll up the concept of asynchronous.

Understanding synchronous / asynchronous issue, we first need to be clear:

JS is single-threaded head iron baby!

Single-threaded popular point that, at the same time can only do one thing. Many online article called " pipeline ", which is very appropriate.
Since learning programming, always known, read and write the code is " made under the self ," which is a very obvious "single-threaded" or "assembly line" feature.

JS main thread

What seems like "main stack" sort of argument, the name to understand it in terms of not the point. If you want to understand with asynchronous I think the best way to be construed as a " thread ", and I shall be understood as: a reading from the code under head iron JS, the main thing it runs. Require long time consuming, is not necessary IO devices (external output device, such as a server request), only js code itself can be run, i.e., the main thread.

JS queue concept

Because js is single-threaded run, like a car by allowing only one-way lane, a section of the code that is a car, a vehicle through lane, you have to line up . This is js the queue . For example the setInterval setTimeout function and, in fact, are asynchronous function, even if the delay setting is 0, will still form a new queue, the queue is placed after the main execution. If the delay is set larger than 0, run queue N millisecond after the main completed.

JS synchronous / asynchronous

Understand the concept and main thread queue, go to understand both synchronous and asynchronous actually like much to understand.

Synchronize:

That is arranged in the main thread code that is synchronized.

asynchronous:

Because some code that takes a long time, or require external help (IO devices, or other I do not know something temporary or special needs), while the single-threaded mechanism js, which takes a long code is easy to plug the code behind for efficiency, it is not very good. Your car breaks down or you want to look easy to let all wait for you to get off the back of the car, it seems too selfish, and too long a delay. So asynchronous came into being, it will be asynchronous code is placed in the main thread, the main thread running when completed, will again run asynchronous code. It corresponds to the formation of another queue, the queue arrangement executed after the main thread. (Asynchronous code may have multiple segments, forming a new queue of the same sort of top-down, it is still a single-threaded js iron head doll)
after the main thread has finished running, will go to perform asynchronous queue should be noted that, asynchronous queues are callback asynchronous method .
Asynchronous meaning itself is to increase the efficiency of the code, the code will take a long time to run independent. It forms a new queue at the same time, it will go to execute code (e.g., request) in an asynchronous, the main queue after execution is completed, the system will go to see asynchronous code is completed, and if completed, the code is called asynchronous callback function, if did not finish, it will wait for the completion of the asynchronous code. In general, asynchronous code callback function has three states: unexecuted , successful and failed .
These are my initial understanding of the synchronous / asynchronous. There may be errors or ambiguous place, I will come back later if found Supplement.

Guess you like

Origin blog.csdn.net/qq_26332449/article/details/94721916