Mini Program's implementation of Promise

App({
  onLaunch: function () {
    var logs = wx.getStorageSync("logs") || []
    logs.unshift(Date.now())
    wx.setStorageSync("logs", logs)
    var that = this
    this.getOpenid().then(()=>{
        return that.setAdmin()
        //console
    })
  },
  getOpenid: function () {
      var that = this
      return new Promise(function (resolve, reject) {
          console.log(resolve)
          wx.getStorage({
              key: "openid",
              success: function (res) {
                 that.globalData.openId = res.data
                 console.log(that.globalData.openId)
              },
              fail: function () {
                  wx.login({
                      success: res => {
                          var code = res.code; //code
                          var appId = "";
                          var secret = "";
                          wx.request({
                              url: "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code",
                              data: {},
                              header: {
                                  "content-type": "json"
                              },
                              success: function (res) {
                                  wx.setStorage({
                                      key: "openid",
                                      data: res.data.openid
                                  })
                                  that.globalData.openId = res.data.openid
                              }
                          })
                      }
                  })
              }
          })
      })
  },
  setAdmin:function () {
      var that = this
      return new Promise(function (resolve, reject) {
          console.log(that.globalData.openId)
          wx.request({
              url: "http://127.0.0.1:8889/api/club/adminComfirm",
              method:"post",
              data:{
                  name:that.globalData.openId
              },
              header:{
                  "content-type":"application/json"
              },
              success:function(res){
                  console.log(that.globalData.openId)
              }
          })
      })
  },
  globalData: {
    userInfo: null,
    openId:null,
    adminOn:true
  }
})

the purpose is to first set openid to data and then pass it to the server for verification, but wxrequst asynchronism requires promise to specify the execution order, but why not execute it after then? find a big answer

Mar.22,2021

 success: function (res) {
                 that.globalData.openId = res.data
                 console.log(that.globalData.openId)
              },

resolve (xxx), is missing in this place

success: function (res) {
                                  wx.setStorage({
                                      key: "openid",
                                      data: res.data.openid
                                  })
                                  that.globalData.openId = res.data.openid
                              }

there is also less resolve (xxx) in this place

then will not be executed if the reslove function is not called.

Menu