简体   繁体   English

如何在 Vue.js 中使用 async/await?

[英]How to use async/await in Vue.js?

I'm new in ES7我是 ES7 新手

I want to use async/await in Vue.js我想在 Vue.js 中使用 async/await

Here is my code这是我的代码

created (){
    this.getA()
    console.log(2)
    this.getB() 
},
methods : {
    getA (){
        console.log(1)
    },
    getB (){
        console.log(3)
    }
}

It returns它返回

1
2
3

But when I use it with axios, then但是当我将它与 axios 一起使用时,然后

created (){
    this.getA()
    console.log(2)
    this.getB() 
},
methods : {
    getA (){
        $axios.post(`/getA`,params){
        .then((result) => {
            console.log(1)
        })
    },
    getB (){
        console.log(3)
    }
}

It returns它返回

2
3
1

So I want to add async/await in that code.所以我想在该代码中添加 async/await。

How can I use async/await?如何使用异步/等待?

I tried我试过

async created (){
    await this.getA()
    console.log(2)
    await this.getB() 
},
methods : {
    getA (){
        $axios.post(`/getA`,params){
        .then((result) => {
            console.log(1)
        })
    },
    getB (){
        console.log(3)
    }
}

It returns same result.它返回相同的结果。

You have to use either then or await not both as shown below:您必须使用thenawait两者,如下所示:

If using then如果使用then

created () {
    this.getA().then((result) => {
            console.log(1)
            console.log(2)
            this.getB()
        })
},
methods : {
    getA () {
        return $axios.post(`/getA`,params);
    },
    getB (){
        console.log(3)
    }
}

If using await如果使用await

async created (){
    await this.getA()
    console.log(1)
    console.log(2)
    this.getB() 
},
methods : {
    getA : async() => {
        return $axios.post(`/getA`,params);
    },
    getB : () => {
        console.log(3)
    }
}

Note that while calling getB() you don't need then or await since it is not asynchronous请注意,在调用 getB() 时,您不需要thenawait因为它不是异步的

Unlike what @thanthu said, you can use both then and await.与@thanthu 所说的不同,您可以同时使用 then 和 await。 In your first post you only missed to add return in the getA method:在您的第一篇文章中,您只是错过了在getA方法中添加return

async created (){
    await this.getA()
    console.log(2)
    await this.getB() 
},
methods : {
    getA() {
        return $axios
            .post(`/getA`,params){
            .then((result) => {
                console.log(1)
            });
    },
    getB() { 
        console.log(3)
    }
}

try this尝试这个

async created (){
    let resultFromgetA = await this.getA()
    console.log(2)
    await this.getB() 
},
methods : {
    getA :() =>{
        return $axios.post(`/getA`,params);
    },
    getB : async() =>{
        //use await key with async calls
        console.log(3)
    }
}

Vuejs , example with axios api request. Vuejs ,带有 axios api 请求的示例。 more details between the code代码之间的更多细节

  methods: {
    async findAndSet(data, type) {
      console.log("This is a API calls, that the response time is vary.");

      const result = await this.getStorages();
      
      console.log("Do what you want after complete the prev job.");
    },
    getStorages() {
      return new Promise(resolve => {

        // Here the point is the resolve that you should resolve('something');.
        this.axios
          .get("/api/sources")
          .then((resp) => {
            this.sources = resp.data;
            resolve('resolved');
          })
          .catch(() => {
            resolve('rejected');
          });
      });
    },
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM