未分类

微信小程序两种风格异步方法调用

默认方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: res => {
this.globalData.userInfo = res.userInfo
console.log(res.userInfo);
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})

promise化通过async和await关键字方式:

1
2
3
4
5
6
7
8
9
10
11
(async () => {
const s = await wxp.getSetting()
if (s.authSetting['scope.userInfo']) {
const res = await wxp.getUserInfo()
this.globalData.userInfo = res.userInfo
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})()

分享到