当前位置:网站首页>JS written test question -- promise, setTimeout, task queue comprehensive question
JS written test question -- promise, setTimeout, task queue comprehensive question
2022-07-25 02:49:00 【H5_ ljy】
setTimeout((n1) => {
console.log(0);
});
new Promise(resolve => {
console.log(1);
setTimeout((n2) => {
resolve();
var p1=new Promise((n1,n2)=>{n1(20)})
p1.then((t1) => console.log(2));
console.log(3);
setTimeout((n3)=>{console.log(9)});
});
new Promise((n1,n2)=>{n1(20)}).then((t2) => console.log(4));
}).then((t3) => {
console.log(5);
var p2=new Promise((n1,n2)=>{n1(20)})
p2.then((t4) => console.log(8));
setTimeout((n4) => console.log(6));
});
console.log(7);
here setTimeout With n1~n4 As parameter marker ;then With t1~t4 As a mark 
Knowledge point :
1.then It belongs to asynchronous micro task ,setTimeout It belongs to asynchronous macro task ,
2. The queue priority of asynchronous tasks is in the same cycle : The asynchronous macro task executes first Then execute asynchronous micro tasks
3. The asynchronous macro task is to start the second thread and wait for the next cycle to start before execution , Asynchronous micro tasks are executed at the end of this cycle
Execution order :
Script run Perform the first macro task :
1. Perform the synchronization task first
2. Add a new macro task to the queue Add a new asynchronous micro task
3. Executing asynchronous micro tasks
Analysis by graph : The first cycle begins : Perform the synchronization task first , So print 1,7,; Then execute asynchronous micro tasks , Print first 4, because then(t3) Not in this cycle ;
Use resolve() To pass in data , So do not execute and wait in the asynchronous micro task queue . The second cycle begins , Execute asynchronous macro task queue , Then a new synchronization task is generated , Print first 0,3; because resolve() Called , therefore then(t3) Execute the generate synchronization task Print 5 , Asynchronous tasks are also generated here , See who gets to the asynchronous task queue first and executes it first ; the reason being that setTimeout(n2) Internally invoked resolve(), therefore then(t1) and setTimeout(n3) In front of each other , Execute asynchronous micro task printing 2,8
Then execute asynchronous macro task printing 9,6

边栏推荐
- Ten year structure and five-year Life-03 trouble as a technical team leader
- Is redis'module'not an internal or external command?
- What should I do when the interface encounters jsonstring
- Jenkins plug-in development -- plug-in expansion
- Permanently mount the image steps
- On the economic model of open source ecology
- Jedispoolconfig parameter configuration from the perspective of source code
- Arduino IDE for raspberry PI Pico development firmware localization installation tutorial
- Flink's study notes
- JS written test question -- deep copy of object
猜你喜欢

R language one page and many pictures

@Retryable @backoff @recover retry the use of annotations

Keil compile download error: no algorithm found for: 08000000h - 08001233h solution

English grammar_ Reflexive pronoun

Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt

String class

How to use blender to make 360 degree panorama and panoramic video?

Mid year summary and personal feelings

Read and upgrade st-link chip information and SWD burning media through STM32 stlink utility tool

Solution to the occupation of project startup port
随机推荐
Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt
6. Object storage
Master jedispoolconfig parameter configuration and learn tuning skills
My creation anniversary (3rd Anniversary)
Generator set work arrangement problem code
What are the basic skills of engineers? How to practice? -- Learning experience sharing "suggestions collection"
MySQL common function summary, very practical, often encountered in interviews
Conceptual distinction between Po, Bo, VO, dto and POJO
After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
[C language] program compilation (preprocessing)
Creating elements of DOM series
"No such plugin: cloudbees folder" solution appears when Jenkins selects the recommended plug-in for the first time
Class notes (4) (2) -- 572. Compete
Rolling division, Young's matrix and three-step flip
Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming
Digital business cloud: how to realize the application value of supplier SRM management system?
C: wechat chat software instance (wpf+websocket+webapi+entityframework)
Common Oracle commands
@Retryable @backoff @recover retry the use of annotations
Object.defineproperty use