Open sindresorhus opened 1 month ago
// @fregante
fetchWithTimeout
I think if you need a reusable function, there's already ky. Timeouts on their own are easy:
const response = fetch('/api', {signal: Abort.timeout(5000)})
I think if you need a reusable function, there's already ky.
You could use that argument on the utilties already here too. Could just use Ky. The point is that this package is composable. You take only what you need.
Timeouts on their own are easy:
Sure, but usually you also want to pass in your own signal
, and then it's more difficult.
You could use that argument on the utilties already here too
throwIfHttpError
is not 4-tokens long, , {signal: AbortSignal.timeout(5000)}
is
this package is composable
👍
I thought it was more like a utilities package like uint8array-extras
than a build-your-own-ky
Sure, but usually you also want to pass in your own
signal
, and then it's more difficult.
A bit, yes
- const fetchWithTimeout = withTimeout(fetch, 5000);
- fetchWithTimeout('/api', {signal: mySignal})
+ fetch('/api', {signal: AbortSignal.any([yourOwn, AbortSignal.timeout(5000)]))
Would be useful to have a function to easily get timeout for a fetch function.
POC (untested):
Feedback wanted
If this one works out, I imagine we could have more.
Alternative API: