How to use async? for hook functions in vue

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

will you use async await, for created in vue or will you output 2 first instead of waiting for 1 to finish?

Mar.29,2021

can achieve this goal in disguise

  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)
  }

you have to understand that all subfunctions are only executed at a specified time, and the framework does not care about the results of their execution, so this approach does not meet your needs.

it is recommended that you implement it according to your needs.

Menu