axios: Timeout doesn't work
Hi, I cant implement timeout request.
What i did:
axios.defaults.timeout = 100000;
My action:
export function login(data) {
return dispatch => {
return axios.post('auth/login', null,{
headers: {
username: data.email,
password: data.password,
},
timeout: 5000
})
.then(res => {
const token = res.data.token;
localStorage.setItem('Token', token);
setAuthToken(token);
dispatch(setAccount(jwtDecode(token)));
return res;
});
};
}
And react side:
this.props.login(this.state).then(
(res) => {
console.log('sign-in');
console.log(res);
browserHistory.push('/student/dashboard');
},
(err) => {
console.log('sign-in error');
console.log(err.response.data);
alert(err.response.data.message);
//isLoading: false
}
);
Where i made mistake?
Thanks for every answers!
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 15
- Comments: 23
Links to this issue
Commits related to this issue
- Use axios CancelToken to ensure timeout (#482) * Revert PR https://github.com/stellar/js-stellar-sdk/pull/465/files * Use axios CancelToken to ensure timeout Axios timeout doesn't catch missing... — committed to stellar/js-stellar-sdk by kalepail 4 years ago
- 修复扫描脚本阻塞 BUG 原因:连接 HVDB 服务器超时 https://github.com/axios/axios/issues/647 — committed to yodhcn/kikoeru-express by yodhcn 4 years ago
I found the
timeout
in axios isresponse timeout
, notconnection timeout
, for example if you connect a local ip address 192.168.11.11 which dose not exist, it will take a long time , the timeout looks like invalid , but if you connect to a normal , well-connected server , the timeout take effects. My platform is react-native.So I solve this problem by the method provided by @camflan , thanks very much , my code :
Still an issue in 2019. I have abandoned the
timeout
option since it is unreliable. Unreliable axios timeout is making my application unreliable. (a single unresolved promise prevents the entire app from continuing).Now I must manually timeout axios using the
CancellationToken
method listed above.I have created this helper function that works for my project:
Wow. I’ve opened this issue almost three years ago and since then people still encounter problems when it comes to timeout. I think the axios team might forgot about this one so next time instead of adding another comment consider opening a new one issue (hopefully that will change anything). You can always attach this as a reference and for now, I’ll close it. Good luck guys!
It seems the problem is not fixed yet. I just encountered on my AWS lambda.
I will try with CancelToken soluce ! Thx
I created a wrapper for this to make is simply throw a timeout error you can catch https://www.npmjs.com/package/@nelsonomuto/axios-request-timeout
Solution of @camflan / @zhuyifan2013 works for me with React-Native 0.41.2 on Android. @zhuyifan2013’s code calls always calls source.cancel(). Writing it like this would make it only cancel when there is no response:
This still isn’t fixed in 0.19.2 btw…
@rshmiraf Is the issue fixed for you?
Is there a reason to think this is fixed? There’s no code change referenced in this or the linked Axios ticket.