admin管理员组文章数量:1356709
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
本文标签: javascriptAxios complete eventStack Overflow
版权声明:本文标题:javascript - Axios complete event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744031325a2578959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论