-
Notifications
You must be signed in to change notification settings - Fork 286
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
any possible way to include timeout on fetch()? #48
Comments
+1 not sure how to add |
yes please, add a |
+1, please add timeout option |
you can do as fellow:
|
This isn't handling a timeout for me... There is an optional "timeout" property you can pass in the options.
|
Create a timeoutPromise wrapper...
You can then wrap any promise...
It won't actually cancel an underlying connection, but will allow you to timeout a promise. |
When a client timeout occured, it's very important to cancel the http request. |
By the way, I solved this neatly in react-native by using redux-saga. |
@raarts how redux-saga solve it? Can you post code in here? |
Inside a saga, race the fetch call with a timeout like this:
|
Kinda late here, this is based off of @tyler-canton code he posted up here
This lets you use fetch like how you would normally, but just have an added timeout option. Thanks @tyler-canton! You might want to change the timeout_err to be an actual error you can check for, for my case this was perfect for what i needed to do. |
@kiorq I think you meant to do this:
Rather than:
Also your example doesn't help because all you do is a fetch. Something like the following would be beneficial:
In my case data came back immediately as undefined and I never get the error message after 3 seconds. Tried to fetch this large JSON file: https://raw.githubusercontent.com/zemirco/sf-city-lots-json/master/citylots.json |
This is how you should be handling abort: const controller = new AbortController()
const signal = controller.signal
setTimeout(() => {
controller.abort()
}, 1000)
fetch(url, { signal }) |
For anyone else who gets hung up on Fetch not having timeout you could use the node-fetch rather than native Fetch API. In a lot of cases a timeout isn't necessary but it is necessary if you want to ensure TCP/IP connections close out and don't just have an open socket for some undetermined time. Here's what I ended up doing with node-fetch Works really well: |
this should be in userLand |
If AbortController is not the solution for you, you can take a peek at this answer on stackoverflow (https://stackoverflow.com/questions/46946380/fetch-api-request-timeout). I found it quite useful ! |
FWIW, I've opened an issue whatwg/fetch#951 with a proposal for a |
@ianstormtaylor You really made a number on every single repo that uses the fetch API. |
couldn't find the docs for this repository...
my code looks like this:
would like to fake a server response for the fetch... any ideas?
thanks in advance
The text was updated successfully, but these errors were encountered: