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
2 Answers
Reset to default 8Following 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