当前位置:网站首页>The execution sequence of async/await, macro tasks and micro tasks
The execution sequence of async/await, macro tasks and micro tasks
2022-07-27 08:57:00 【znhyXYG】
One 、async
belt async Keyword function , Is to declare asynchronous functions , The return value is promise object , If async The keyword function does not return promise, Will use automatically Promise.resolve() packing .

The return value is Promise {<resolved>: "test"}.
Two 、await
await Wait for the result of the expression on the right , The result is promise Object or other value .
If it doesn't wait for one promise object , that await The result of this expression is what it's waiting for .
If it waits for one promise object ,await And he got busy , It blocks the rest of the code , Waiting for the promise object resolve, Then get resolve Value , As await The result of an expression .
because test() Blocking caused by ,console.log('end') Will wait for two seconds to execute
So in order to avoid blocking ,await Must be used in async Function ,async Function calls do not block .
3、 ... and 、async/await Execution order of
encounter await Will block the following code , Execute first async External sync code , Synchronization code execution completed , Back to async Inside , Carry on await Later code .( Understand the above instructions well . in other words ,await It will only block the current async The code inside the method , It will not affect the execution of external code )

Execution results :

First, execute the macro task , perform test1 function , perform console.log('statr test1')
· encounter await, Execute the right first test2 Medium console.log('test2'), Interrupt the following code , perform test1 External sync code
· perform console.log('start async');
· encounter setTimeout, Push to the next macro task queue
· perform Promise Synchronization code inside console.log('promise1')
· Run to the promise().then, Found to be promise object , Push to the micro task queue
· perform console.log('end async')
· test1 After the execution of the external synchronization code , go back to test1 in ,console.log(await test2()) Return... After execution Promise {<resolved>: "return test2 value"}, yes promise object , Push to the micro task queue
· At this point, the first macro task ends , Perform all the micro tasks , Because the micro task queue is first in first out , So first execute console.log('promise2'), After execution console.log('return test2 value')
· perform test2 After completion , The following code is no longer blocked , perform console.log('end test1');
· Execute the next macro task , The perform console.log('setTimeout');
Four 、 Common macro tasks and micro tasks
Macro task :script、setTimeOut、setInterval、setImmediate
Micro task : promise.then,process.nextTick、Object.observe、MutationObserver
Be careful :Promise It's a synchronization task
The execution order of macro tasks and micro tasks :
- Execute macro task script,
- Get into script after , All synchronous tasks are executed by the main thread
- All macro tasks are put into the macro task execution queue
- All the micro tasks are put into the micro task execution queue
- Clear the micro task queue first ,
- Take another macro task , perform , Then clear the micro task queue
- In turn, cycle
边栏推荐
- The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
- Deep understanding of Kalman filter (3): multidimensional Kalman filter
- How to permanently set source
- Redis network IO
- Solution of database migration error
- 691. 立方体IV
- Pyqt5 rapid development and practice 4.1 qmainwindow
- The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
- Aruba learning notes 10 security authentication portal authentication (web page configuration)
- 接口测试工具-Jmeter压力测试使用
猜你喜欢

Deep understanding of Kalman filter (2): one dimensional Kalman filter

Understand various IOU loss functions in target detection

CUDA programming-01: build CUDA Programming Environment

Deep understanding of Kalman filter (3): multidimensional Kalman filter

User management - restrictions

【进程间通信IPC】- 信号量的学习

Deep understanding of Kalman filter (1): background knowledge

【Flutter -- GetX】准备篇

PyTorch自定义CUDA算子教程与运行时间分析

MySQL Express
随机推荐
What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
4279. 笛卡尔树
General Administration of Customs: the import of such products is suspended
[I2C reading mpu6050 of Renesas ra6m4 development board]
Full Permutation (depth first, permutation tree)
Include error in vs Code (new header file)
2036: [Blue Bridge Cup 2022 preliminary] statistical submatrix (two-dimensional prefix sum, one-dimensional prefix sum)
Sharing of four open source face recognition projects
Do a reptile project by yourself
Encountered 7 file(s) that should have been pointers, but weren‘t
[flutter -- geTx] preparation
微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
How to optimize the deep learning model to improve the reasoning speed
Cache consistency and memory barrier
Aruba learning notes 10 security authentication portal authentication (web page configuration)
693. 行程排序
String type and bitmap of redis
HUAWEI 机试题:字符串变换最小字符串 js
NIO this.selector.select()
Matlab 利用M文件产生模糊控制器