几句代码理解async/await的用法
运行一下几句代码,立马就能理解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')
},