鍍金池/ 問答/Java  HTML/ 小程序 Promise執(zhí)行問題

小程序 Promise執(zhí)行問題

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啊,沒有執(zhí)行
    })
  },
  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
  }
})

目的是要先 設(shè)置openid到data后,再傳給服務(wù)器驗證,但是wxrequst異步要promise來規(guī)定執(zhí)行順序啊,但是這里為什么then后不執(zhí)行呢,求大大解答

回答
編輯回答
青黛色
 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)

不調(diào)用reslove函數(shù),then是不會執(zhí)行的

2017年10月27日 23:25