微信小程序异步处理promise使用方法

微信小程序异步处理我使用的是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等等

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值