Open HenrikZabel opened 9 months ago
I also encountered this today.
Edit: It seems that RN just doesnt work with AbortSignal.timeout
(yet), maybe its due to hermes idk but I did create a drop-in replacement for fetch
that implements timeout
as part of the native options
export const fetch = async (
url: string,
{ timeout = 5000, ...fetchOptions }: RequestInit & { timeout?: number } = {}
) => {
const controller = new AbortController();
const abort = setTimeout(() => {
controller.abort();
}, timeout);
const response = await globalThis.fetch(url, {
...fetchOptions,
signal: controller.signal,
});
clearTimeout(abort);
return response;
};
Note that this will just throw an AbortError, if you want any more control over the exception thrown, make sure to wrap the fetch with a try catch and handle the exceptions accordingly
The same goes for AbortSignal.any()
. :(
Description
I want to abort a fetch request after 5 seconds. I am using following code:
Steps to reproduce
React Native Version
0.73.1
Affected Platforms
Runtime - Android
Output of
npx react-native info
Stacktrace or Logs
Reproducer
https://github.com/HenrikZabel/abortsignal-bug/
Screenshots and Videos
No response