A polyfill for React Native's whatwg-fetch
's mirror.
This adds support for timeout
as one of the fetch
options.
import fetch from 'react-native-fetch-polyfill';
fetch(url, {timeout: 30 * 1000})
.then(response => {
// a successful response
})
.catch(error => {
// an error when the request fails, such as during a timeout
})
React Native's XMLHttpRequest
interface exposes a timeout property sent to the RCTNetworking
module, as well as an abort method. fetch
does not expose access to this by default, this polyfill allows specifying a timeout
within the options.
This value attached to NSMutableURLRequest
, where the native networking layer will enforce the timeout rule.
The result of the timeout being reached will result in a promise rejected with a `TypeError('Network rqeuest failed').
Fetch is a networking abstraction above XMLHttpRequest
. It reflects the WHATWG fetch specification and can be found in whatwg/fetch. It is the networking library used in React Native.
Fetch has two challenges:
- It cannot be externally aborted (whatwg/fetch#27 and whatwg/fetch#447)
- It does not support
timeout
(facebook/react-native#2394, facebook/react-native#2556, whatwg/fetch#20, JakeChampion/fetch#175)
Why are these not supported? As a fetch
maintainer points out in JakeChampion/fetch#68 (comment), the spec does not describe a standard for this behavior.
The polyfill picks out specific pieces of whatwg/fetch required to apply the patch.
The tagged version of the polyfill corresponds to the version of fetch
that it patches.
When new versions of fetch
are released, the polyfill will be updated and tagged.