wx.getSetting()方法是异步么?为何会导致死循环呢
我有尝试将这个授权作为一个单独的方法,并通过一个简单的while循环来进行保证最后的结果一定是用户授权,可是最后却发现会导致死循环,代码会一直console.log(userInfo),代码块如下:
function checkSetting() { var userInfo = false while (!userInfo) { console.log(userInfo) wx.getSetting({ success: res => { console.log( "res.authSetting['scope.userInfo']=" +res.authSetting[ 'scope.userInfo' ]) userInfo = res.authSetting[ 'scope.userInfo' ] } }) //如果没有获取到授权 if (!userInfo) { wx.showModal({ title: '提示' , content: '为保证程序正常运行,请允许小程序获取您公开的用户信息' , showCancel: false , confirmText: '去授权' , success: res => { if (res.confirm) { wx.openSetting({ success: res => { console.log( "2es.authSetting['scope.userInfo']=" + res.authSetting[ 'scope.userInfo' ]) userInfo = res.authSetting[ 'scope.userInfo' ] } }) } } }) } }
|