Promise同步使用

Promise同步使用

微信小程序中使用Promise

想要向后台请求一个接口后实现后续代码顺序执行,由于自己封装的接口没有写回调函数,有时候会出现上面的请求返回结果还未收到,下面的代码就先执行了,所以用Promise来实现请求等待。

new  Promise(async (resolve, reject) => {
    
    
	await wx.User.login({
    
    
		....
	})
	// 通过resolve来向后面then传值,then用res来接收
	resolve(wx.getStorageSync('userinfo').type)
}).then(res => {
    
    
		if (res == 1) {
    
    
           	...
		} else if (res == 2) {
    
    
			...
		}else {
    
    
          ...
		}
	// 实现加载完请求后将loading隐藏
	wx.hideLoading({
    
    
		success: (res) => {
    
    },
	})
})

猜你喜欢

转载自blog.csdn.net/yh0016/article/details/110469421