http icon indicating copy to clipboard operation
http copied to clipboard

Abort requests on native targets

Open jeromebon opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe. On the web, fetch have an abort mechanisms, the AbortSignal According to the documentation there is the webFetchExtra parameter which accept a signal. But there doesn't seem to be an equivalent for native targets. Is this by design or an oversight?

I usually abort requests in React's useEffect to avoid the "Can't perform a React state update on an unmounted component" error. For example:

useEffect(() => {
  const abortController = new AbortController()
  fetch(url, { signal: abortController.signal })
    .then(data => {
      setState(data)
    })
  return abortController.abort
}, [])

Describe the solution you'd like For the native targets to support the signal parameter, which would accept an AbortSignal instance such as on the web. Or for Http.request (get, post, etc) to return both the response's Promise and a cancel method.

Describe alternatives you've considered Implementing abort on top of Http by wrapping the Http promise in an other Promise which reject whenever the signal emit the abort event

jeromebon avatar Oct 28 '21 13:10 jeromebon

I left a PR (#193) trying to solve this. It's actually a little complicated 😅, since we need to track down the abortable requests and close their respective sockets. Currently, it's not working on iOS, though.

dfidalg0 avatar Nov 10 '21 15:11 dfidalg0