当前位置:网站首页>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
边栏推荐
- LaTeX学习笔记(一)——安装配置
- JS - 鼠标键盘配置 及 浏览器禁止操作
- C2 random generation function seed, numpy. Random. Seed(), TF. Random. Set_ Seed Learning + reprint and sorting
- flex布局
- C语言从入门到入土——数组
- 作用域与作用域链
- 4. 在屏幕上绘制一个红色三角形,一个黄色正方形。三角形在后,小;正方形在前,大。使用融合技术,使得可以透过正方形看到三角形,源和目标融合因子分别为GL_SRC_ALPHA和GL_ONE_MINUS
- JS:为什么 [] == ![] 返回 true ?
- Some thoughts on being a professional
- C语言进阶篇 四.自定义类型详解
猜你喜欢
随机推荐
Scikit learn notes
总结Browser对象
C语言进阶篇 六.文件的操作
统计学之样本和总体的关系: 样本成功比例+中心极限定理(样本均值)
Pure white tutorial using Druid database connection pool in idea
16进制转rgb
关于numpy基础用法的一个整理
关键字_02break与continue
useRef 创建动态引用
C语言进阶篇 四.自定义类型详解
THREE——OrbitControls轨道控制器
A collation of the basic usage of numpy
Promise续(尝试自己实现一个promise)更详细的注释和其它接口暂未完成,下次咱们继续。
Detailed explanation of string constant pool and intern() of string
深度剖析数据在内存中的存储
程序员工具合集!(转载)
利用二分法从数组中寻找具体数值
输入若干数据,找出最大值输出。(键盘和文件读取)
Learn AI linear regression from Li Mu. The code implementation from scratch is super detailed
node连接mysql,使用navicat可视化









