简体   繁体   English

避免在“then”和“catch”中重复相同的代码

[英]Avoid repeating the same code in 'then' and 'catch'

Is there an option to avoid repeating this.$router.go() in the code below and run a piece of code whatever the result is?是否有一个选项可以避免在下面的代码中重复this.$router.go()并运行一段代码,无论结果如何?

await axios.put(`/user/` + this.data.id, this.user)
  .then((response) => {
    this.$router.go();
  })
  .catch((error) => {
    this.$router.go();
  });

You can put it into a named function ahead of time:您可以提前将其放入一个名为 function 的文件中:

const handle = () => {
  this.$router.go();
};
await axios.put(`/user/` + this.data.id, this.user)
  .then(handle)
  .catch(handle);

You could also use .finally , if Axios supports it, it's a bit new , but a problem with .finally is that the Promise will "pass through" the .finally , so although you'll be able to call this.$router.go without repetition, if the axios call rejects, you'll end up with a rejected Promise.你也可以使用.finally ,如果 Axios 支持它,它有点新,但是.finally的一个问题是 Promise 将“通过” .finally ,所以虽然你可以调用this.$router.go没有重复,如果axios调用被拒绝,你最终会得到一个被拒绝的 Promise。 so you'll need to .catch afterwards to avoid the await from throwing:所以你需要.catch之后避免抛出await

await axios.put(`/user/` + this.data.id, this.user)
  .finally(() => {
    this.$router.go();
  })
  .catch(() => {});

...and run a piece of code whatever the result is? ...并运行一段代码,无论结果如何?

In Axios you can do like this as per the official docs在 Axios 你可以按照官方文档这样做

axios.get('url')
  .then(function (response) {
    // handle success
  })
  .catch(function (error) {
    // handle error
  })
  .then(function () {
    // always executed
    this.$router.go();
  });

Update: Checked this after comment by @CertainPerformance更新:@CertainPerformance 发表评论后对此进行了检查

Axios now supports finally (Check under examples) Axios 现在终于支持了(查看示例)

What you actually mean is this:你的实际意思是这样的:

await axios.put(`/user/` + this.data.id, this.user)
  .then((response) => {
    this.$router.go();
  },(error) => {
    this.$router.go();
  });

because the only difference between the code you wrote and this one is: if this.$router.go() throws an error, then call this.$router.go() again.因为您编写的代码与此代码之间的唯一区别是:如果this.$router.go()抛出错误,则再次调用this.$router.go() Doesn't make much sense.没有多大意义。

And since you don't care about the response either, you can as well write:而且由于您也不关心response ,因此您也可以编写:

await axios.put(`/user/` + this.data.id, this.user)
  .catch(() => {})
  .then(() => {
    this.$router.go();
  });

or better (IMO)或更好(IMO)

await axios.put(`/user/` + this.data.id, this.user).catch(noop);

this.$router.go();

assuming you have defined function noop(){} somewhere else for further use.假设您在其他地方定义function noop(){}以供进一步使用。

Even if it's said that you should not mix promise.then() and await , I prefer this over即使有人说你不应该混合promise.then()await ,我更喜欢这个

try {
  await axios.put(`/user/` + this.data.id, this.user);
} catch (error) {}

this.$router.go();

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

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