How to solve the problem of Mini Program's onload,onlaunch sequence

when Mini Program was first developed, he thought at first that onload, was not after onlaunch had been executed, and then the necessary condition judgment was always the approximate code segment of false, first:

app.js

onLaunch: function () {
    //
    this.userLogin()
},
userLogin: function () {
    // 
    wx.login({
      success: res => {
        //  res.code  openId, sessionKey, unionId        
        if (res.code){
          wx.request({
            url: "login.php", 
            data: {
              code : res.code
            },
            method : "POST",
            header : {
              "content-type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              getApp().globalData.hasAuthorize = true;
            },
            fail: function () {
              console.log("!")
            },
          })
        } else {
          console.log("!" + res.errMsg)
        }
      }
    })
  },
 globalData: {
    userInfo: null,
    hasAuthorize: false
  }

index.js

onLoad: function () {
    //
    this.setData({
      hasAuthorize: app.globalData.hasAuthorize
    })
  },

my question:

very often, when you open Mini Program, the onLoad of index.js has already been executed before the onLaunch is finished. As a result, hasAuthorize gets the initial value of app.globalData.hasAuthorize (false)

every time.

is there any way to wait for onLaunch to finish in onLoad? Beginners ask for advice, thank you.

Jul.09,2021

the problem you encounter is not that onLoad is executed before onLaunch , but that the asynchronous operation callback in onLaunch triggers later than onLoad (which is inevitable).

so there is a very simple solution:
instead of getting app.globalData.hasAuthorize directly from onLoad , expose a function, pass in the callback, and set hasAuthorize in the callback.

Code like this:

{
  // ...
  onLoad () {
    this.getAuthorize().then(res => {
      this.setData({
        hasAuthorize: res
      })
    })
  }
  getAuthorize () {
    const { globalData } = getApp()
    return new Promise((resolve, reject) => {
      if (globalData.loaded) {
        resolve(globalData. hasAuthorize)
      } else {
        //  userLogin  success resolve
        
        {
          success () {
            globalData.hasAuthorize = true
            
            //  resolve +  flag
            
            globalData.loaded = true
            resolve(true)
          }
          
          fail () {
            reject()
          }
        }
      }
    })
  }
  // ...
}
The
pseudocode example is for reference only, so that the logic of onLaunch can be deleted without having to worry about sending requests repeatedly.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e8c1-2c409.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e8c1-2c409.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?