当前位置:网站首页>JS foundation -- task queue and event loop
JS foundation -- task queue and event loop
2022-07-25 02:49:00 【H5_ ljy】
List of articles
Task queue
Task refers to js Running code in code .
JS There are two kinds of tasks in : Synchronous tasks and asynchronous tasks
Synchronous task refers to : Tasks on the main thread are queued for execution
Asynchronous task means : Do not enter the main thread , It's going into a " Task queue " The task of , When the task in the main thread is completed , Will take asynchronous tasks from the task queue and put them into the main thread for execution
Asynchronous tasks can be divided into : Asynchronous macro task and asynchronous micro task .
Asynchronous macro tasks such as : Timers, etc . Asynchronous micro task :Promise etc. . Officially ;
There are many ways to implement asynchronous patterns , More commonly used are :
(1) Delay class :setTimeout、setInterval、requestAnimationFrame、setImmediate.
(2) Monitor the type of event implementation : monitor new Image Loading state 、 monitor script Loading state 、 monitor iframe Loading state 、Message.
(3) With asynchronous function type Promise、ajax wait .
Case study
setTimeout(()=>{ //setTimeout Is to add an asynchronous macro task Wait for the synchronous task and asynchronous micro task to execute
console.log(11111)
},1000)
let p=new Promise((n1,n2)=>{
n1(22222)
})
p.then((n1)=>{ //Promise Of then The method is to add an asynchronous micro task , Execute after this round of synchronization task is executed
console.log(n1)
})
console.log(33333) // Synchronization task , The first to perform Print 33333

The event loop
When javascript When the code is executed, different variables will be stored in different locations in memory : Pile up (heap) And the stack (stack) To distinguish . among , There are some objects in the pile . There are some basic type variables and object pointers in the stack .
When we call a method ,js An execution environment corresponding to this method will be generated , Also called execution context . And when a series of methods are called in turn , because js It's single threaded , Only one method can be executed at a time , So these methods are queued up in a separate place . This place is called Execution stack .
js The engine does not wait for an asynchronous event to return results , It will suspend the event , Continue to perform other tasks in the execution stack .
When the asynchronous event returns the result ,js This event will be added to a different queue than the current execution stack , We call it Event queue ( Task queue ).
Being put into the event queue does not immediately execute its callback , Instead, wait for all tasks in the current execution stack to finish , When the main thread is idle , The main thread will look up whether the event queue has tasks .
If there is , Then the main thread will take out the events in the first place , And put the callback corresponding to this event into the execution stack , Then execute the synchronization code …, So again and again , This creates an infinite cycle . This is the process called “ The event loop ” Why .
The execution stack is a running container similar to the function call stack . When the execution stack is empty ,JS The engine checks the event queue , If it's not empty , The event queue pushes the first task into the execution stack to run .
This kind of operation of constantly checking the event queue , Become The event loop .
边栏推荐
- "No such plugin: cloudbees folder" solution appears when Jenkins selects the recommended plug-in for the first time
- "Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?
- How to use blender to make 360 degree panorama and panoramic video?
- HAC cluster is modified to stand-alone
- JS written test question -- deep copy of object
- Jenkins configuration plug-in interface displays "suggestions collection" in Chinese
- Object.defineproperty use
- JS foundation -- JSON
- Keepalivetime=0 description of ThreadPoolExecutor
- Details of happens before rules
猜你喜欢

Tp5.1 include include files (reference public files)

What are you working for? Don't give up is our only choice, come on, everyone

Object.defineproperty use
![[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality](/img/12/1763571a99e6ef15fb7f9512c54e9b.png)
[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality

MySQL common function summary, very practical, often encountered in interviews

Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port

Custom types in C language

JS written test question -- deep copy of object

Sequence diagram of UML diagram series

TS uses a third-party library, and there is no type declaration file error handling
随机推荐
Strategy mode, just read one article
[jailhouse article] scheduling policies and system software architectures for mixed criticality
Visualization of correlation coefficient matrix
Tp5.1 initialize initialization method (not \u initialize)
Experienced the troubleshooting and solution process of an online CPU100% and the application of oom
Tp5.1 paging (with parameter transfer)
JS foundation -- JSON
Insertion of balanced binary tree (AVL tree)
Case analysis of building exhibition service management system with low code development platform
The file in scanpy1.9.1 cannot be read in scanpy1.7.2. The problem is solved
Inheritance (prototype)
Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt
Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
Domestic edge computing organization and product research
It7259q-13, it7259ex-24 feature wearable devices
Cloudrev deploy your own public cloud disk (pagoda installation method)
English grammar_ Reflexive pronoun
Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming
Cookies and sessions
Beginners must see the markdown User Guide