小程序 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
  }
})

目的是要先 设置openid到data后,再传给服务器验证,但是wxrequst异步要promise来规定执行顺序啊,但是这里为什么then后不执行呢,求大大解答

阅读 2.3k
1 个回答
 success: function (res) {
                 that.globalData.openId = res.data
                 console.log(that.globalData.openId)
              },

这个地方少写了resolve( xxx ),

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

这个地方同样少写了resolve(xxx)

不调用reslove函数,then是不会执行的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题