JavaScript Execution Sequence Event Loop

Source: Internet
Author: User
Tags event listener

JavaScript is a single-threaded language that enables the main thread to be non-blocking, but can simulate multi-threaded operations with the event loop
Synchronous asynchronous task execution sequence in Event loop:

    • All asynchronous tasks are registered in the event table, and when the specified time is complete, the event table puts the function into the event queue, and the main thread's synchronization task executes to the event queue to read the corresponding function and into the main thread execution.

The JS engine monitoring the process and executes the function in the event queue when it discovers that the main process execution stack is empty

let= [];$.ajax({    url:url    data:data,    success:=>{        console.log(‘发送成功!‘);    }})console.log(‘代码执行结束‘);

The above is the Ajax execution order:
Ajax enters event Table, registering the callback function success.
Execute Console.log (' End of Code execution ').
The Ajax event completes and the callback function success into the event Queue.
The main thread reads the callback function success from the event queue and executes.

In addition to the generalized synchronous and asynchronous tasks, we have a finer definition of the task:

MACRO-TASK (Macro Task):: SetTimeout, SetInterval, setimmediate, I/O, UI interaction events
Micro-task (Micro Task): Promise, Process.nexttick, Mutaionobserver

Console.Log(' 1 ');SetTimeout(function(){    Console.Log(' 2 ');    Process.Nexttick(function(){        Console.Log(' 3 ');    })New Promise(function(resolve){        Console.Log(' 4 ');        Resolve();    }). Then(function(){        Console.Log(' 5 ')})})Process.Nexttick(function(){    Console.Log(' 6 ');})New Promise(function(resolve){    Console.Log(' 7 ');    Resolve();}). Then(function(){    Console.Log(' 8 ')})SetTimeout(function(){    Console.Log(' 9 ');    Process.Nexttick(function(){        Console.Log(' Ten ');    })New Promise(function(resolve){        Console.Log(' One ');        Resolve();    }). Then(function(){        Console.Log(' a ')})})

The entire code, a total of three event loops, the full output is 1,7,6,8,2,4,3,5,9,11,10,12.
(Note that the event listener dependency Libuv in the node environment is not exactly the same as the front-end environment, and the output order may have errors)

JavaScript Execution Sequence Event Loop

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.