最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Axios complete event - Stack Overflow

programmeradmin0浏览0评论

I'm using Axios for API services and just curious if there's any official way to handle a "plete" event as we have used in Ajax call.

So like

axios.get('/v1/api_endpoint?parameters')
  .then((res) => { .. })
  .catch((err) => { .. })
  plete(() => {})     //  <== is there any way to handle this plete event? 

I'm using Axios for API services and just curious if there's any official way to handle a "plete" event as we have used in Ajax call.

So like

axios.get('/v1/api_endpoint?parameters')
  .then((res) => { .. })
  .catch((err) => { .. })
  .plete(() => {})     //  <== is there any way to handle this plete event? 
Share Improve this question asked Nov 13, 2020 at 17:44 brucelinbrucelin 1,0212 gold badges12 silver badges25 bronze badges 1
  • developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Brian Lee Commented Nov 13, 2020 at 17:47
Add a ment  | 

2 Answers 2

Reset to default 8

Following the axios documentation here, the secondary .then() is the one that I'm looking for.

Here's a good example of how to handle that axios plete event which will always be executed whether it has succeeded or failed.

axios.get('/v1/api_endpoint?with_parameters')
  .then((res) => { // handle success })
  .catch((err) => { // handle error })
  .then(() => { // always executed })        <-- this is the one

If you have to check whether an API call is a success or not, then you can use below code:

const response = await axios.post(
      "http://localhost:8000/xyz",
      { token, user }
    );
    const status = response.status
    if (status == 200) {
      console.log('Success')
      toast("Successful Transaction", { type: "success" });
    } else {
      console.log('Falure')
      toast("Falure", { type: "error" });
    }

You can also use finally to check if the event is pleted or not. Attaching the link for your reference: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally

发布评论

评论列表(0)

  1. 暂无评论