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

USE*_*SER 4 javascript async-await vue.js ecmascript-2017

我是ES7的新手

我想在Vue.js中使用async / await

这是我的代码

created (){
    this.getA()
    console.log(2)
    this.getB() 
},
methods : {
    getA (){
        console.log(1)
    },
    getB (){
        console.log(3)
    }
}
Run Code Online (Sandbox Code Playgroud)

它返回

1
2
3
Run Code Online (Sandbox Code Playgroud)

但是当我与axios一起使用时

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

它返回

2
3
1
Run Code Online (Sandbox Code Playgroud)

所以我想在该代码中添加异步/等待。

如何使用异步/等待?

我试过了

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

它返回相同的结果。

小智 13

与@thanthu 所说的不同,您可以同时使用 then 和 await。在你的第一篇文章,你只错过了添加returngetA方法:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你可以。但你应该吗?有一天,你会发现自己处于 `try-catch` 和 `.catch(err => {console.log(err)}) ` 块的中间,其中有大量的 `async-await` 和 `.then` 和 ` .all`指令。对于绅士来说,这可能是第二大困难的挑战。当然,在尝试生孩子之后。对于女性来说 - 这是最困难的。 (4认同)
  • 感觉仍然是要走的路,你正在_等待_一个在 `console.log(1)` 运行时结束的承诺 (2认同)

Tha*_*thu 8

您必须同时使用thenawait不使用两者,如belwo所示:

如果使用 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

如果使用 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,在调用getB()时不需要thenawait由于它不是异步的


Nas*_*imi 8

Vuejs,带有Axios API 请求的示例。代码之间的更多细节

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

      const result = await this.getSources();
      
      console.log("Do what you want after completing the prev job.");
    },
    getSources() {
      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');
          });
      });
    },
  },
Run Code Online (Sandbox Code Playgroud)


sat*_*mar 6

尝试这个

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)
    }
}
Run Code Online (Sandbox Code Playgroud)