async/await
1) 理解
简化 Promise 对象的使用: 不在使用 then() 来指定回调函数.
如果失败用 async/await 怎么使用: try...catch catch 得到的就是 promise 里失败的 不用 try..catch 则 await 拿到的就是 then
同步编码方式方式实现异步流程
2) 使用
哪里使用 await? 写在 promise 左侧 也就是写在一个返回 promise 对象的表达式的左侧: 左侧得到的不再是 Promise, 而是 promise 异步成功的值
哪里使用 async? await 所在最近函数定义的左侧
- async test(){
- return 1
- //throw new Error() //promise 返回一个失败的状态
- }
- const promise = await test() //promise=1
await 得到的值为 async 函数返回的值
来源: http://www.bubuko.com/infodetail-3279771.html