Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Alternative of using try catch for an axios request when there is an error that returns nothing?

At the moment, if I use

const response = await '$nuxt.axios.delete/account'; //pretend request
if(response) { 
  console.log('response', response); //logs if successful
  //response is {data: "", config:{transitional:{...}, status:204, statusText:""}
}
else { 
    console.log('error'); //will not log if there is an error
}

there is no response at all if there is an error. I want to check if it has been successful or not, so when it is successful I can then push the object into the array, so I won’t have to make a new request to get the added piece of data.

I could use try and catch, which does return an error, but then I’d have to push the object into the array assuming it is successful, and only then delete it from the array if an error is caught. This seems a bit redundant.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way to get the response without using try catch or catch(error) ?

>Solution :

but then I’d have to push the object into the array assuming it is successful, and only then delete it from the array if an error is caught

You could instead toggle a flag then which tells you whether you got something to add, and do the actual adding afterwards.

let flag_do_not_add = false;
try {
  /*request*/
} catch(e) {
  flag_do_not_add = true;
}
if(!flag_do_not_add) {
  /*add to array*/
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading