创建Vue承诺功能

时间:2019-01-22 16:30:02

标签: javascript api vue.js axios

是否可以在Vue JS中为Javascript Promise创建函数。

我有多个对API的Axios调用,并且每个都有两个Promise。每个Axios呼叫承诺都是相同的。是否可以创建一个在我的axios调用完成后被调用的函数。

axios.delete(url {data: item})
    .then((response) => {
        return response.data;
    })
    .then((data) => {
        axios.get(url)
        .then((response) => {
            this.name = response.data
    }).catch((error) => {
       console.log(error);
       alert("There was an error, please try again.")
    });
})

 axios.post(url, JSON.stringify(item))
    .then((response) => {
         return response.data;
     })
     .then((data) => {
         axios.get(url)
         .then((response) => {
          this.test = response.data
   }).catch((error) => {
      console.log(error);
      alert("There was an error, please try again.")
   });
 })

可能是这样吗?

axios.post(url, JSON.stringify(item))
    .then(runThisFunction(response,data,error))
})

runThisFunction(response,data,error) {
   //Something like this but 
   .then((response) => {
         return response.data;
     })
     .then((data) => {
         axios.get(url)
         .then((response) => {
          this.test = response.data
   }).catch((error) => {
      console.log(error);
      alert("There was an error, please try again.")
   });
}

0 个答案:

没有答案