当前位置:网站首页>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

边栏推荐
- Mp4 package analysis
- Work arrangement of generator set
- Inheritance (prototype)
- Introduction to web security telent testing and defense
- Physical experiment simulation
- Rolling division, Young's matrix and three-step flip
- How to switch terminators in PostgreSQL?
- Large number processing -- use case
- Wechat sports field reservation of the finished works of the applet graduation project (5) assignment
- Creating elements of DOM series
猜你喜欢

Introduction to web security telent testing and defense
![[C language] program compilation (preprocessing)](/img/94/175a84d89b1f16987e529eb029cbbc.png)
[C language] program compilation (preprocessing)

Project management tool Zen

YouTube Download and (batch) Download

After six years of precipitation, new consumption has found a digital transformation paradigm

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

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

JS foundation -- math

Redux best practices "Redux toolkit"

Conceptual distinction between Po, Bo, VO, dto and POJO
随机推荐
Visualization of correlation coefficient matrix
Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
Keil compile download error: no algorithm found for: 08000000h - 08001233h solution
Componentization and modularization
Go common standard library -time
Details of C language compilation preprocessing and comparison of macros and functions
Keepalivetime=0 description of ThreadPoolExecutor
Get to know string thoroughly
Consul cluster deployment
H5 common positioning function package
Digital commerce cloud fine chemical industry management platform integrated informatization solution
[C language] program compilation (preprocessing)
6. Object storage
Vulntarget vulnerability shooting range -vulntarget-b
【C】 Advanced knowledge of file operation
Can PostgreSQL CDC only connect to the main database? Connection from the library reports an error logical decoden
"Introduction to interface testing" punch in to learn day05: how can a testing framework support restful interfaces?
Rolling division, Young's matrix and three-step flip
StrError and PERROR
DLL load failed: the page file is too small to complete the operation