简体   繁体   English

解决给定方法内的所有承诺后执行方法

[英]Execute method after all promises inside of a given method resolved

In a Vue.js component, I have some methods that use axios to call an API. 在Vue.js组件中,我有一些使用axios调用API的方法。

In different cases, I need to execute some code once the call in this method has resolved, but I don't want to add a bunch of if statements in the .then() chained to the axios call. 在不同情况下,一旦此方法中的调用已解决,我就需要执行一些代码,但是我不想在.then .then()添加一堆if语句,这些语句链接到axios调用。

methods: {
  callApi() {
    axios.get('/api')
     .then(() => {
       // call has resolved, request is done
     })
  },
  firstMethod() {
    this.callApi()
    // Need to wait for the call to resolve
    // Do something
  },
  secondMethod() {
    this.callApi()
    // Need to wait for the call to resolve
    // Do something else
  }
}

As you can see, firstMethod and secondMethod both rely on callApi but are supposed to do a different thing once the request is done. 如您所见, firstMethodsecondMethod都依赖于callApi但是一旦请求完成,它们应该做不同的事情。 I prefer to split this logic in different functions instead of using conditions in the callApi method. 我更喜欢将此逻辑拆分为不同的函数,而不是在callApi方法中使用条件。 Is there a way to do this without having to add this logic inside of the callApi ? 有没有一种方法可以不必在callApi内部添加此逻辑?

Have callApi return the promise chain, then use and return that in firstMethod and secondMethod . callApi返回承诺链,然后在firstMethodsecondMethod使用并返回它。

methods: {
  callApi() {
    return axios.get('/api')
     .then(() => {
       // call has resolved, request is done
     })
  },
  firstMethod() {
    return this.callApi()
    .then(() => {
      // Need to wait for the call to resolve
      // Do something
    })
  },
  secondMethod() {
    return this.callApi()
    .then(() => {
      // Need to wait for the call to resolve
      // Do something else
    })
  }
}

Whatever calls callApi , firstMethod , or secondMethod should check for failure and handle/report it. 任何调用callApifirstMethodsecondMethod都应检查故障并处理/报告故障。


Your original code was breaking one of the rules of promises: The function should always either return the chain, or handle rejection. 您的原始代码违反了promise的规则之一:函数应始终返回链或处理拒绝。 (And yes, that's or [99.9% of the time], not and .) (是的,这是 [的99.9%的时间],不 。)

Promises chain, so you need to take the promise that Axios returns, perform any handling you might need to do, then return it from your callApi method. 承诺链,因此您需要保证Axios返回,执行您可能需要执行的所有处理,然后从callApi方法返回它。 In your other methods where you're calling callApi you handle the returned promise and put any code that has to run after the API has responded in the handler function. 在调用callApi其他方法中,您将处理返回的callApi ,并将在API响应后必须运行的所有代码放入处理程序函数中。

callApi() {
  return axios.get('/api')
   .then(() => {
     // this gets handled first
   })
},
firstMethod() {
  this.callApi()
  .then(() => {
    // this gets handled second
  })
},
secondMethod() {
  this.callApi()
  .then(() => {
    // or this gets handled second
  })
}

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

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