欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-08 09:24:49
...

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

相关标签: promise使用方法