WeChat Mini Programs request Asynchronous assignment problem

I wrote the login / acquisition user information in app.js. After success, I assigned the content to the onLoad of globalData.xxxxx,Page and then assigned the globalData.xxxxx to data.xxxxx. I found that the assignment was not so bad. Many materials say that the problem of onLoad,onLaunch,request asynchronous request leads to how to solve it.

app.js

app({

globalData: {
    xxxxx: null
  },

onLaunch: function () {
    var that = this;
    wx.login({
      success: function (res) {
        var code = res.code;
        if (code) {
          wx.getUserInfo({
            success: function (res) {
              wx.request({
                .....
                },
                success: function (data) {
                  that.globalData.xxxxx = data.data.xxxxx
                },
                fail: function () {
                  console.log("")
                }
              })
            },
            fail: function () {
              console.log(",")
            }
          })
        } else {
          console.log("" + res.errMsg)
        }
      },
      fail: function () {
        console.log("")
      }
    })
  }

})

index.js

var app = getApp();

Page({
    data: {
        xxxxx:null
      },
      
    onLoad: function (options) {
        this.setData({
          xxxxx: app.globalData.xxxxx
        });
    }

})

use Promise to encapsulate the method of login in APP.js, call the method encapsulated in app in index.js, get the desired value, and then assign the global globalData. If you still don't understand, I'll give you the code when I come to the company tomorrow.

Menu