Vue中async await的使用示例

想要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的使用示例)