These three concepts have been learned in recent contact with Nodejs. These three concepts in 14, Nanyi teacher wrote a blog, the spirit of the great God on this basis to improve, and
For the small rookie, is simply a fairy dialogue, do not understand, do not understand.
Nanyi: http://www.ruanyifeng.com/blog/2014/10/event-loop.html
Flutter: 40143961.
Follow up to see an article, the personal feel write better https://www.cnblogs.com/liangyin/p/9783342.html
function calls stack: (Call stack)
Index.js
Features: Execute the pre-stack, execute after the stack. A first out of a stack, followed by B out of the stack, and then the entire global context out of the stack, become empty stack.
Task queues: (Task queue)
Queue features: FIFO first.
In single-threaded JS, tasks are divided into synchronous tasks and asynchronous tasks.
Synchronization task: A task that is queued on the main thread to perform the latter task only if the previous task has completed execution.
Asynchronous task: Do not enter the main thread, executed by another thread, put the callback function into the task queue or the event queue, after execution, trigger the callback function, the callback function is added to the function call stack, followed by the previous stack.
The output is "1" "2" "Set2" "SetTimeout"
The first step: SetTimeout is an asynchronous task, the callback function joins the task queue, and the main thread performs the subsequent tasks first.
Step two: For Loop sync task, Console.log ("1") advanced stack, post out stack.
Step three: SetTimeout is consistent with the first step.
Fourth step: For Loop Sync task Console.log ("2") Advanced Warfare. After the stack.
The fifth step: After the stack task is completed, it will constantly poll, the callback function in the task queue, callback function re-enter the main thread of the call stack, output data, complete the stack.
Event Polling:
is to repeat the steps above, as long as the main thread stack executes, the callback function in the queue of the execution task will be taken. Complete the stack and stack.
Finally, attach a picture to understand:
function call stack, task queue, event polling