微信小程序異步處理我使用的是promise處理,這樣能夠讓小程序執行起來邏輯更加清晰明瞭,示例代碼如下:
let promise1 = new Promise(function (resolve, reject) {
wx.getImageInfo({
src: 'https://www.qiaolibeilang.com//public/uploads/images/20180809/free.png',
success: function (res) {
resolve(res);
}
})
});
let promise2 = new Promise(function (resolve, reject) {
var uid = wx.getStorageSync('uid')
console.log(uid)
wx.getImageInfo({
src: that.data.src1 + uid + that.data.src3,
success: function (res) {
resolve(res);
}
})
});
Promise.all([
promise1, promise2
]).then(res => {
var that = this
var winWidth = wx.getSystemInfoSync().windowWidth;// 獲取當前設備的可視寬度
var winHeight = wx.getSystemInfoSync().windowHeight;// 獲取當前設備的可視高度
that.setData({
winWidth: winWidth,
winHeight: winHeight
})
console.log(winWidth)
console.log(winHeight)
const ctx = wx.createCanvasContext('post')
ctx.setFillStyle('red')
ctx.drawImage(res[0].path, 0, 0, that.data.winWidth, that.data.winWidth * 1.778)
ctx.drawImage(res[1].path, that.data.winWidth - 160, that.data.winWidth * 1.778 - 105, 80, 80)
console.log(res)
ctx.draw(true)
})
},
這是我做的一個海報,上面的圖片地址是錯誤的,具體你們需要執行什麼功能就看你們自己寫了,還可以有promise3、promise4等等