当前位置:网站首页>JS knowledge blind spot | understanding of async & await
JS knowledge blind spot | understanding of async & await
2022-06-21 07:09:00 【A touch of sunshine &】
async await The implementation process of
Never paid attention to await This keyword , Only know await How to use 、await Is similar to the resolve The role of successful callback , In the previous implementation, through new Promise(resolve => resolve()), But after optimization, it becomes Promise.resolve(). Ask questions about the cycle of events during the interview .
The following questions do not relate to async and Promise Priority judgment of , Just common synchronous and asynchronous judgment , To re recognize Generator+Promise == async And await The process of
setTimeout(() => {
console.log(0)}, 0)
{
//Node It is not allowed to write self executing anonymous functions directly , Should be placed in curly braces
(async function(){
console.log(1)
await Promise.resolve()
console.log(2)
})()
}
console.log(3)
new Promise(reslve => {
console.log(4)
}).then(res => {
console.log(5)
}).catch(err => {
console.log(6)
})
const p = new Promise((resolve, reject) => {
console.log(7)
resolve(8)
})
p.then(res => {
console.log(res)
}).then(res => {
console.log(9)
})
p.then(res => {
console.log(10)
})
{
(async () => {
console.log(11)
await Promise.resolve(333)
console.log(12)
})()
}
analysis
Be careful :async The decorated function is a function executed synchronously , But when it comes to await,await After that, all code blocks become micro tasks , If we meet again await, This is the last await Of then The task , Micro tasks , At this point, the generator and Promise Explain in combination .
Review generator
Use
function*Definition , Another special syntax in the class*[Symbol.iterator](){} || *iterator(){}It's OK, too . By keyword yield Split code blocks , By keyword yield Divided code blocks are like functions one by one .
- Generators are special iterators
- adopt yield Control code operation ,yield It's like a function , Able to receive and return .
const msg = yield 'Hello JS'function request(msg) { return new Promise((resolve, reject) => { resolve(msg) }) } function* getBanners() { // Every time yield All represent then Chained code blocks let msg = yield "Hello JS"; msg = yield request(msg + "aaa"); msg = yield request(msg + "bbb"); msg = yield request(msg + "ccc"); console.log(msg); } // Manual implementation , Match each step with Promise Connect const generator = getBanners("Hello JS"); Promise.resolve(generator.next().value).then((res) => { generator.next(res).value.then((res) => { generator.next(res).value.then((res) => { generator.next(res).value.then((res) => { generator.next(res); // Finally, you can call }); }); }); }); // automatically ( How many times does the recursive method yield, Just calculate ) let val = generator.next().value /* initial next */ function co(generator) { if (val === undefined) { /* base case */ return ; } if (val instanceof Promise) { val.then(res => { val = generator.next(res).value co(generator) }) return ; } val = generator.next(val).value co(generator) } co(generator)
- After analyzing this stage , Basically, it realizes async and await The function of , Simply speaking yield Namely await The function of , At the same time, it can be found from the implementation ,await Just like that. resolve The grammar sugar of , If it is a normal value, then wrap a layer
Promise.resolveCan finish Promise Transformation of objects . - summary :async And await == Promise + Generator await The following code will be in the form of micro tasks .
answer
The main thing is async and await Synchronous and asynchronous ( Micro task , Than Promise Higher priority ).
1
3
4
7
11
2
8
10
12
9 Be careful : This is because the arrow function returns the last line , There is a return value , Even if there is no explicit resolve
0
边栏推荐
- Google Earth Engine(GEE)——美国本土岩性数据集
- 使用cell ranger进行单细胞转录组定量分析
- How to deal with the error message of concurrentmodificationexception?
- 天气预报小程序源码/天气类微信小程序源码
- Tweenmax oscilloscope 3D animation
- Construction methods of file class and various common methods
- C language program design - Sanzi chess (semester homework)
- Trick or treat SVG Halloween JS special effect
- Introduction to exceptions
- 154 Solana distribution token
猜你喜欢
![[FPGA wavelet transform] Verilog implementation of image 9/7 integer wavelet transform based on FPGA](/img/e2/c33848f3952ce0a3bf0d315fb0f78c.png)
[FPGA wavelet transform] Verilog implementation of image 9/7 integer wavelet transform based on FPGA

Ztmao theme cat WordPress Theme classic lost version /wp website template download station source code + global SEO function setting

Understand this point

Feature scaling for machine learning

Do you know all the extension racks of ThinkPHP?

Ztmao主题猫wordpress主题经典失传版/WP网站模板下载站源码+全局SEO功能设定

Quantitative analysis of single cell transcriptome using cell Ranger

微信小程序_5,全局配置

如何利用MES管理系统实现防错和预警

How to see who developed the applet (see the method of the applet development company)
随机推荐
Analysis of source code encryption products
flutter jpush
【3】 AI studio project details - single machine multi machine training distributed training - Parl
Weather forecast applet source code / weather wechat applet source code
2022年大学英语六级6月翻译
js操作Cookie,js设置Cookie值,js读取Cookie值
AdEx 治理投票:质押奖励减半
JS operation cookie, JS setting cookie value, JS reading cookie value
數據分析之:不同行業的常見指標
Wechat applet_ 3. Wxml template syntax
【GNN】GNN图神经网络工具箱的应用和matlab仿真
【osg】OSG开发(03)——构建MSVC版的osgQt库
win10上vs2017配置Eigen3开发环境
153 Solana create PDA and storage
Building a hard core Gateway - resume
BOL波尔独创双币驱动模型 引领DeFi+NFT+Web3.0新热点
Postman发布API文档
GEO2R:对GEO数据库中的数据进行差异分析
[GNN] Application of GNN neural network toolbox and MATLAB simulation
Pyg tutorial (6): customizing the messaging network