Synchronous use of Promise

Synchronous use of Promise

Using Promise in WeChat Mini Program

If you want to request an interface from the background to implement subsequent code execution in sequence, because the interface you encapsulate does not write a callback function, sometimes the above request return result has not been received, and the following code will be executed first, so Promise is used. Implement request waiting.

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) => {
    
    },
	})
})

Guess you like

Origin blog.csdn.net/yh0016/article/details/110469421