Js's question about async await?

created () {
  this.init()
},
methods: {
  init () {
    this.getToken()
    this.getData()
  },
  async getToken () {
    let token = await postToken()
    ...
  },
  async getData () {
    if(this.token) return "token no null"
    let data = await postData();
    ....
  }
}
The

code is roughly the same as above, and the actual runtime always says, "token no null", has already added async await to two methods that use ajax, why is it still asynchronous?

Mar.29,2021

that's inside the getToken method and the getData method, but your init method is not.


Async/Await itself is used to handle asynchronism. After await execution, it will return

as soon as there is a result.
function timeout(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}

async function asyncPrint(value, ms) {
    await timeout(ms);
    console.log(value);
}

asyncPrint('hello world', 50);

the above code specifies that hello world is output after 50 milliseconds.

if you want to specify its request time, the return value can set the wait time.
reference:
ES6 series articles
understands the async/await of JavaScript


  async init () {
    await this.getToken()
    this.getData()
  },
Menu