Home > Net >  How can I use conditionally make an API call using AXIOS? [closed]
How can I use conditionally make an API call using AXIOS? [closed]

Time:09-17

I'm using AXIOS with REACTjs I want a functionality that if my first API(GET) call returns Not Found status code then I want to send the other call efficiently. Although I'm getting some data through that .catch method in form of Promise<pending> .Please Help!!!

CodePudding user response:

Why don’t you add a conditional in the catch block to check the status?

For example:

axios.get('/first-call')
  .then(resp => { 
     //handle successful response
  })
  .catch(error => {
    if (error.response && error.response.status === 404) {
     // make second API call
    }
  });
  • Related