async函數返回值

methods: {
  start () {
    console.log(this.test()) // Promise {<fulfilled>: 123}
  },
  async test () {
    return 123
    // return Promise.resolve(123)
    // return new Promise((res, rej) => {
    //     res(123)
    // })
  }
}

若是直接執行this.test()(無論直接return 123仍是return Promise.resolve(123)),都將返回Promise對象(會被包裝爲一個當即resolve的Promise對象);async

拿到return值的方式:
1.Promise.thenthis

start () {
  this.test().then(res => {
    console.log(res) // 123
  })
}

2.async-awaitcode

async start () {
  console.log(await this.test()) // 123
}
相關文章
相關標籤/搜索