Method a calls method b with a post request, why can't you get the value of the variable in method b?

1. Method createAnimation calls method getToken,getToken sends a post request and assigns the returned parameter to the variable token, in data (), but the value of token cannot be obtained with this.token in createAnimation

2,

createAnimation() {
    const t = this;
    this.viewDialog = true;
    this.viewTitle = this.anaInput;
    t.loading = true;
    t.getToken()
    // console.log(t.getToken());
    // this.$options.methods.getToken.bind(this)();
    console.log(222,t.token)
},

getToken() {
    // this.token="qqqq";
    this.$http.post(this.ip + "/getyoyatoken", { //token
        userid: this.user_id
    }).then((res) => {
        if (res.data.data.result === 1) {
            this.token = res.data.data.list[0].token;
            // return res.data.data.list[0].token;
            console.log(111,this.token)
        }
    })
},

clipboard.png

3. According to Baidu"s use of this.$options.methods.getToken.bind (this) (); or this.$options.methods.getToken (); does not work, and error post undefined, I tried to assign values to token directly in getToken, which can be obtained in createAnimation. It is true that the value of the variable token has been changed and cannot be obtained in the createAnimation method, but you can get the value of token by performing other methods after executing createAnimation, and I tried to use setTimeout (() = > {this.getToken}). If you directly ask getToken to return back the response result, the received result will be printed out in createAnimation as undefined

.

4. What is the reason for this?

5, add:

async createAnimation() {
    const t = this;
    this.viewDialog = true;
    this.viewTitle = this.anaInput;
    t.loading = true;
    t.token = await t.getToken();
    console.log(222,t.token)
}

you can"t get token, if you write it this way, because you can call the getToken method in many places, so how can it be easier? you don"t have to rewrite those lines of code in getToken

.
Apr.26,2021

getToken is an asynchronous operation that waits for a return when executed.
console.log belongs to Synchronize and does not wait for getToken to finish execution, but executes immediately. Since getToken has not yet returned data, it is undefined.
if you hit a breakpoint and follow, you will understand.


can you rely on the spectrum? isn't your http request asynchronous? No wonder it can be printed out


about 5. Supplement.
await's method (getToken) should return a Promise, or getToken or Synchronize

getToken() {
    // this.token='qqqq';
    return this.$http.post(this.ip + '/getyoyatoken', { //token
        userid: this.user_id
    }).then((res) => {
        if (res.data.data.result === 1) {
            this.token = res.data.data.list[0].token;
            // return res.data.data.list[0].token;
            console.log(111,this.token)
        }
        return Promise.resolve(this.token)
    })
}
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-1bf805b-47106.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-1bf805b-47106.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?