Vue中async await的使用示例

Little heaven發表於2018-12-21

想要await按照預期來執行,就必須是promise

  methods: {
    getAll() {
      return new Promise((resolve, reject) => {
        this.$axios.get(eventApi.getAllUsers).then(response => {
          let a = response.data[0];
          console.log(a);
          resolve(a);
        });
      });
    },
    getlog() {
      return new Promise((resolve, reject) => {
        console.log("111");
        resolve("111");
      });
    },
    getLog2() {
      console.log("222");
    },
    getlog3() {
      setTimeout(() => {
        console.log("333");
      }, 100);
    },
    getlog4() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log("444");
          resolve();
        }, 1000);
      });
    },
    async getAllData() {
      await this.getlog();
      await this.getlog4();
      await this.getAll();
      await this.getlog3();
      await this.getLog2();
    }
複製程式碼

結果如下

Vue中async await的使用示例
不理解的請留言!哈哈哈,我會告訴你?

相關文章