Skip to content
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

[fetch] how to set fetch's timeout ? #2394

Closed
hufeng opened this issue Aug 21, 2015 · 12 comments
Closed

[fetch] how to set fetch's timeout ? #2394

hufeng opened this issue Aug 21, 2015 · 12 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@hufeng
Copy link

hufeng commented Aug 21, 2015

dear all,

how to set fetch's timeout ?

@simondelacourt
Copy link

Found this;

{
method: 'GET'
, headers: {} // request header, format {a:1} or {b:[1,2,3]}
, follow: 20 // maximum redirect count, 0 to not follow redirect
, timeout: 0 // req/res timeout in ms, 0 to disable, timeout reset on redirect
, compress: true // support gzip/deflate content encoding, false to disable
, size: 0 // maximum response body size in bytes, 0 to disable
, body: empty // request body, can be a string or readable stream
, agent: null // custom http.Agent instance
}

as options for https://github.com/bitinn/node-fetch

Maybe worth trying, as far as I know the react native fetch is based on the default fetch api, so this might work..

@brentvatne
Copy link
Collaborator

dupe #2556

@adambene
Copy link

@simondelacourt RN uses whatwh-fetch runtime so there is no timeout by default. I have a workaround (whatwg-fetch-timeout package): #2556 (comment)

I am really curious about how you enthusiast RN users out there build robust apps without having timeout and abort().

@thecodecafe
Copy link

This answer was quite helpful, worked like a charm.
Answer

@adambene
Copy link

@mrbarde It does not abort the pending HTTP request.

@thecodecafe
Copy link

@adambene yh it doesn't and that's a pain in the neck, I just use it so I can update my UI for the user.

@adambene
Copy link

@mrbarde Yeah, agree.

@dzpt
Copy link

dzpt commented Jul 29, 2017

@simondelacourt it doesn't work with react
Any better solution?

@WardianGames
Copy link

I would enclose my fetch requests with async functions or promises. Then set a timeout, on the parent if the promise has not returned by the time the timeout does, stop waiting for it to return and move on.

@adambene
Copy link

@WardianGames it is okay for get methods but in the case of post it would be essentisl to be able to abort the pending request after a given timeout.

@sebringj
Copy link

sebringj commented Nov 9, 2017

I'm doing a hack with XMLHttpRequest and return it on the returned request with the "abort" method attached to the returned promise that has a closure to the xhr object. The calling code can then assign itself the request and abort it later. Not elegant but works. Chaining in promises basically breaks it unless you get the first reference to the request.

I use it like this.

if (this.request) this.request.abort()
this.request = someApiCall(data)
let results = await request

where this.request is held on to and aborted if more requests are made. Still experimenting with this but seems to work for not queuing up too many requests so far.

The code is kind of like this but still tweaking.

function retch(url, params) {
  let xhr = new XMLHttpRequest()
  let promise = new Promise((resolve, reject) => {
    // do your xhr stuff here with resolve, reject
  })
  promise.abort = () => xhr.abort()
  return promise
}

@sananddev
Copy link

i am still searching for the solution using fetch and timeout but..... it gets me searching all day

@facebook facebook locked as resolved and limited conversation to collaborators Jul 21, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

10 participants