async/await 深度理解使用

在vue中使用

  • eg
  async created () {
    await setTimeout(()=>{
      console.log(1)
    },5000);
  },
  async mounted () {
    console.log(2)
  }

在vue中给created使用async await,还是会先输出2,而不是等1输出完?
若想实现这个要求, 可变相使用

  async created () {
    this.create_promise = new Promise(resolve=>this.create_promise_resolve=resolve);
    setTimeout(()=>{
        console.log(1);
        this.create_promise_resolve();
    },1000)
  },
  async mounted () {
    await this.create_promise;
    console.log(2)
  }

async/await 的理解使用

posted @ 2019-05-25 12:14  仲灏  阅读(527)  评论(0编辑  收藏  举报