几句代码理解async/await的用法
发布于 2 年前 作者 houjie 2818 次浏览 来自 分享

运行一下几句代码,立马就能理解async/await怎么使用了:

  testAsync: async function () {
    let res = await wx.showModal({ content: 'step1?' })
    if (res.confirm) { } else return
    await this.step1()
    res = await wx.showModal({ content: 'step2?' })
    if (res.confirm) { } else return
    await this.step2()
    console.log('end')
  },
  step1: async function () {
    await wx.showModal({ content: 'this is step1' })
    console.log('end of step1')
  },
  step2: async function () {
    let res = await wx.showActionSheet({
      itemList: ['A', 'B'],
    }).catch(err => console.log('err:', err))//catch可以防止阻断
    if (res.tapIndex == 0) { console.log('A') }
    if (res.tapIndex == 1) { console.log('B') }
    console.log('end of step2')
  },

回到顶部