当前位置:网站首页>asyncawait和promise的区别
asyncawait和promise的区别
2022-07-29 05:20:00 【本本的香菜】
async
async函数定义
async函数是使用关键字声明的函数。async 是“异步”的简写,所以应该很好理解 async 用于申明一个 function 是异步的。
作用
用于解决:异步程序产生的bug
####async函数的语法
async function name([param[, param[, ... param]]]) {
statements
}
参数:name 函数名称
param 要传递给函数的参数的名称
statements 包含函数主体的表达式 ,可以使用await
返回值: 一个全新的promise,这个promise要么会通过一个由async函数返回的值被解决,要么会通过一个从async函数中抛出的(或其中没有被捕获到的)异常被拒绝
下面为async的具体使用实例:
async function foo(p) {
console.log("foo run",p);
return 1;
}
var res = foo(1);
console.log(res);//{<fulfilled>: 1}
async函数一定会返回一个promise对象。如果一个async函数的返回值看起来不是promise,那么它将会被隐式地包装在一个promise中。
例如,如下代码:
async function foo() {
//promise.[[promiseValue]]
return 1
}
//等价于
function foo() {
return Promise.resolve(1)
}//{<fulfilled>: 1}
下面是对async的一些补充:
async 本身是一个语法糖—>语法糖:带有一定功能的关键字
- 语法糖的作用:能够减少代码量、增加程序的可读性,从而减少程序代码出错的机会
async 函数中 return 值如何接受
方式一
通过 promise.then-cb 形参获取
async function foo(){
console.log(222222);
return 123;
}
let res = foo();
res.then(data=>{
console.log(data);//123
})
方式二
第二种接受函数返回值的方式是 await
(async function (){
console.log('async run');
//第二种接受函数返回值的方式是 await
let res = await foo();
console.log(res);
})()
async function foo(){
console.log('foo run');
return 123;
}
//res作用:接受 async foo函数返回值 是promise
let res = foo();
await
await定义
await 的意思是等待,所以应该很好理解,await 等待某个操作完成。
作用
await关键字的作用 就是获取 Promise中返回的内容, 获取的是Promise函数中resolve或者reject的值(await 作用是获取promise.[[promiseValue]]的值)
关于await的注意点
await 必须写在 async 中
await 后是一个promise实例对象
[[promiseValue]]
[[PromiseValue]]是个内部变量,外部无法得到,只能在then中获取。
[promiseValue]哪些能赋值
- async函数的return
- new promise 中 resolve实参
- then 中 return (catch finally 中的return)
- promise.reslove()实参 promise.reject()实参
三者的区别
- promise和 async/await都是解决异步编程的一种方式,但是async/await使得异步代码看起来像同步代码。
- 函数前面多了一个async关键字。await关键字只能用于async定于的函数内。async函数会隐式地返回一个Promise,该promise的resolve值就是return的值。
为什么async/await更好?
使用async函数可以让代码简洁很多,不需要像Promise一样需要then,不需要写匿名函数处理Promise的resolve的值,也不需要定义多余的data变量,还避免了嵌套代码。
async/await 让 try/catch可以同时处理同步和异步的错误。在下面的示例中,try/catch不能处理JSON.parse的错误,因为它在Promise中,我们需要使用.catch,这样的错误会显得代码非常的冗余。
边栏推荐
- “山东大学移动互联网开发技术教学网站建设”项目实训日志四
- Laravel service container (Application of context binding)
- Training log 7 of the project "construction of Shandong University mobile Internet development technology teaching website"
- datax安装
- File文件上传的使用(2)--上传到阿里云Oss文件服务器
- Fantom (FTM) 价格将在未来几天飙升 20%
- Training log III of "Shandong University mobile Internet development technology teaching website construction" project
- July 28 ens/usd Value Forecast: ENS attracts huge profits
- Go|Gin 快速使用Swagger
- Windows下cmd窗口连接mysql并操作表
猜你喜欢

php写一个购买全网最低价的纸尿裤

Common prompt pop-up box of uniapp

运动健康深入人心,MOVE PROTOCOL引领品质生活

Laravel服务容器(上下文绑定的运用)

Power BI Report Server 自定义身份验证

“山东大学移动互联网开发技术教学网站建设”项目实训日志五

性能对比|FASS iSCSI vs NVMe/TCP

Laravel service container (inheritance and events)

Idea using JDBC to connect mysql database personal detailed tutorial

Markdown语法
随机推荐
微信内置浏览器禁止缓存的问题
“山东大学移动互联网开发技术教学网站建设”项目实训日志四
闪贷Dapp的调研及实现
Machine learning makes character recognition easier: kotlin+mvvm+ Huawei ml Kit
与张小姐的春夏秋冬(5)
How can Plato obtain premium income through elephant swap in a bear market?
“山东大学移动互联网开发技术教学网站建设”项目实训日志三
Under the bear market of encrypted assets, platofarm's strategy can still obtain stable income
rsync+inotyfy实现数据单项监控实时同步
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
Win10+opencv3.2+vs2015 configuration
山寨币SHIB 在 ETH 鲸鱼的投资组合中拥有 5.486 亿美元的股份——交易者应提防……
华为2020校招笔试编程题 看这篇就够了(下)
麦当娜“Hellbent”购买130万美元的nft无聊猿,现在被认为太贵了
Idea using JDBC to connect mysql database personal detailed tutorial
Training log 6 of the project "construction of Shandong University mobile Internet development technology teaching website"
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
深度学习的趣味app简单优化(适合新手)
Markdown语法
C# 判断用户是手机访问还是电脑访问