The question about promise

a () {
    return new Promise((res, rej) => {
        // :
        getData().then(res => {
            this.data = res.data.reslut
            console.log(this.data)
        })
    });
}

the a method is defined in vue , and the a method is called in the click event of a button. The desired effect is that after the a method is executed, the alert is executed as follows:

click () {
    this.a().then(()=> {
        alert(11)
    })
}

in this way, the a method is in effect, but the following alert (11) is not triggered. Is something wrong? Or the writing is fundamentally wrong, please give me some advice.

Mar.01,2021

a () {
    return new Promise((resolve,reject) => {
        getData().then(res => {
            this.data = res.data.reslut
            console.log(this.data)
            resolve(this.data);
        })
    });
}

you didn't change the promise state with resolve, so you didn't go to the then callback. The code has been given above, so I won't repeat it again.


resolve changes the current state of the promise object to success, and reject turns the current state of the promise object into a failure, and the execution of then will not continue until the state changes.

Menu