当前位置:网站首页>Promise_ Async and await
Promise_ Async and await
2022-07-24 05:31:00 【Good [email protected]】
Small mistakes in thinking
There was such a question before , I'm working on functions 1 Use in await Waiting for asynchronous execution , In function 2 Calling function in 1, That function 2 The synchronization code in will wait for the function 1 Do you want to execute after execution ?
Here's an example : call listChenge function , Print first 111 Or print it first 222 Well ?
async awaitMethod(){ const res = await new Promise((resolve,reject)=>{ setTimeout(()=>{ console.log('111') resolve() },5000) }) console.log('333') }, listChenge(){ this.awaitMethod() console.log('222') }
The answer is to print first 222, wait for 5 Second printing 111,333
The reason is simple —>await Will suspend At present async Execution of a function , wait for promise Execute the following code after execution ;
If we want to print first 111, Re print 222
async awaitMethod(){ const res = await new Promise((resolve,reject)=>{ setTimeout(()=>{ console.log('111') resolve() },5000) }) console.log('333') }, async listChenge(){ await this.awaitMethod() console.log('222') }
async And await
Due to the small mistake above , Review again async And await Knowledge point ;
effect
Use async/await You can write forms Sync Code to handle asynchronous technological process ;
Concept
await
Be careful :await Operators can only be used in async Use in a function , Otherwise, an exception will be thrown ;
‘await’ is only allowed within async functions
grammar :
[ Return value ] = await expressionThe expression can be a Promise Object or any value to wait for ;
Promise object —> The return value is Promise Processing results of ;
resolveAfter2Seconds(x) { return new Promise(resolve => { setTimeout(() => { resolve(x); }, 2000); }); }, async listChenge(){ const res = await this.resolveAfter2Seconds(1) console.log(' result ', res) // 1 }
Not Promise object —> The return value is the value itself
async listChenge(){ const res = await 1 console.log(' result ', res) // 1 }
await Expression will Pause the current async Execution of a function , wait for Promise Processing is complete , And go on with it async function
- promise-fulfilled—> Carry on async function;
- promise-rejected—> take Prmoise The exception reason of is thrown ;
async function
Definition : Use async The function declared by the keyword is called async function ;
grammar :
async function Function name (){}
Return value
Return to one Promise
If the return value is not a promise,async The function will wrap the value as a Promise
async function test(){ return 222 } // Equivalent to async function test(){ return new Promise(){ resolve(222)} }
If there is no return value , The default value is undefined
async And await Use a combination of
- async in 0 One or more await;
- await The expression pauses the entire async The execution process of a function and cedes its control , Only if its waiting is based on promise The process will not resume until the asynchronous operation of is honored or rejected .promise The solution value of is treated as the solution value of await Return value of expression
版权声明
本文为[Good [email protected]@@]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207240515391504.html
边栏推荐
猜你喜欢
随机推荐
构造函数_Map构造函数
Generator generator, which generates only two methods
C语言进阶篇 四.自定义类型详解
reflex
模板数据的二次加工
Redis的使用
WIX 路径中带空格
函数_概括
special effects - 鼠标点击,出现随机设置的文字
详解浏览器和Node的事件循环机制及区别
Detailed explanation of string constant pool and intern() of string
JS:为什么 [] == ![] 返回 true ?
This is the first article
面向 对象
C语言起步
C语言进阶篇 七.程序的编译和预处理
Programmer tools collection! (Reprinted)
special effects - 樱花飘落背景特效
02-移动端页面适配
key的作用是什么









