- 异步调用 callback hell
- Promise then catch 链式调用,但也是基于回调函数
- async/await是同步语法,彻底消灭回调函数
// 使用 async 匿名函数// 应为使用 await 必须使用 async(async function() {const img1 = await loadImg(src)})()
注意
const src = 'xxxx'// 由于第一行没有写分号,所以第三行的匿名函数要写一个【叹号】,否则会当作第一行的延续, 即'xxxx'()(),从而导致报错!(async function() {const img1 = await loadImg(src)})()
async/await 和 promise 的关系
- async/await是消灭异步回调的终极武器
- 但和 promise 并不互斥
- 两者相辅相成
- 执行async,返回的是 promise 对象
- await 相当于 promise 的 then
- try…catch 可以捕获异常,代替了 promise 的 catch
