function call stack, task queue, event polling

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.