-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timeout doesn't work #647
Comments
Timeout doesn't work too on react native 0.39 |
Same problem. |
Same issue here with Chrome (Windows). Chrome seems to ignore any timeout setting. I'm setting it when making a request like this: axios({
url: 'myurl',
method: 'post',
data: data,
timeout: 1000
}) |
Same, timeout doesn't work - I've implemented my own timeout functionality using CancelToken. I'd love to remove this cancelling boilerplate |
Still doesn't work on me either using React Native 0.42.3 .. |
Sure! My actual implementation is using redux-saga, which might be confusing if you're not used to using generators. If you are, I'm happy to show that too. Here's a quickly written, slightly contrived, (and never executed 😄 ) example for you that should help. import { axios, CancelToken } from "axios"
function updateUserInfo(data, cancel) {
return axios
.post(URL, data, {
// CancelToken takes a func that has the cancel function for it's param
// we'll set it to a prop on our cancel object that we passed in. Then we'll
// call this function where/when we need to cancel this request
cancelToken: new CancelToken(c => (cancel.exec = c)),
})
.then(r => r) // return the response
.catch(err => {
throw err // throw our error because the request failed
})
}
function submitForm(formData) {
// declare our cancel object to hold the cancel func.
// this has to be an object to exploit JS passing objects by reference and not value
let cancel = { exec: null }
const results = updateUserInfo(formData, cancel)
.then(resp => console.log(resp))
.catch(err => {
throw err
})
setTimeout(() => {
if (!results.ok) {
// if the timeout fires, and our results didn't come back
// then we'll call the cancel func set by CancelToken
cancel.exec()
}
}, 5000)
} cc @MrLyfing |
same issue!! |
I found the So I solve this problem by the method provided by @camflan , thanks very much , my code : const connectToServer = ip => {
let source = CancelToken.source();
setTimeout(() => {
source.cancel();
}, 300);
return axios.get(ip + '/config', {cancelToken: source.token}).then((result) => {
// My logic
})
}; |
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: function loginButtonPressed(username, password) {
return async (dispatch) => {
const source = CancelToken.source();
try {
let response = null;
setTimeout(() => {
if (response === null) {
source.cancel();
}
}, 4000);
dispatch(authenticationPending());
response = await axios.post('auth/login',
{username, password},
{cancelToken: source.token});
dispatch(authenticationSuccess());
} catch (error) {
dispatch(authenticationFailed());
}
};
} |
@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. |
same issue! Sometimes |
Still an issue in 2019. I have abandoned the Now I must manually timeout axios using the I have created this helper function that works for my project: const config = {
timeout: 1000
}
const axiosGet = (url, options = {}) => {
const abort = axios.CancelToken.source()
const id = setTimeout(
() => abort.cancel(`Timeout of ${config.timeout}ms.`),
config.timeout
)
return axios
.get(url, { cancelToken: abort.token, ...options })
.then(response => {
clearTimeout(id)
return response
})
}
// example usage
axiosGet(`${url}/status.json`) |
I created a wrapper for this to make is simply throw a timeout error you can catch
|
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! |
This still isn't fixed in 0.19.2 btw... |
* Revert PR https://github.com/stellar/js-stellar-sdk/pull/465/files * Use axios CancelToken to ensure timeout Axios timeout doesn't catch missing urls, e.g. those with no response so we use the axios cancel token to ensure the timeout. axios/axios#647 axios/axios#1503 * adjustments to get tests to pass
Hi, is your problem on nodejs platform? From the previous comments on this issue it looks like related to browser or mobile environments. |
@epatrasc You can reproduce it like that :
|
I solved the problem by adding timeout into the axios configuration before data parameter. For some reason, timeout parameter will not work if placed after the data parameter.
|
Hey thanks for the reply and the example. |
I have tried with both timeout before and after but same result, there's a timeout but after ~1min (timeout is set to 100ms). I used @arfa123's example code for testing. this is the result:
|
It seems the problem is not fixed yet. I will try with CancelToken soluce ! Thx |
BUG 原因:连接 HVDB 服务器超时 axios/axios#647
I am still facing this issue in our node app. |
Hi,
I cant implement timeout request.
What i did:
axios.defaults.timeout = 100000;
My action:
And react side:
Where i made mistake?
Thanks for every answers!
The text was updated successfully, but these errors were encountered: