Re: [whatwg/fetch] Add a `timeout` option, to prevent hanging (#951)

@sibelius More constructive please? Saying "do it in userland" misses the entire point of this issue. 

It's already _possible_ to do in userland—I know that. But people aren't doing it correctly because the API doesn't have good UX. Most of the popular code samples that show adding timeouts to fetch do it incorrectly, and leave open the infinite hanging failure mode. And that's not even counting the code that just doesn't add timeouts because `fetch`'s current ergonomics don't make it easy. 

It's **extremely** hard to debug these infinite hangs when they happen, because they can happen at any layer of the callstack above where `fetch` is called. Any `async` function that has a dependency call a buggy `fetch` can hang indefinitely. 

It's critical that people use timeouts. And to make that happen it's critical that it be easy.

Asking people to master entirely new `AbortController` and `AbortSignal` concepts for the 99% use case of timing out a request is not a smart thing to do if you're looking to help people write stable code. (Nothing wrong with those two concepts, they just shouldn't be involved in the common case because _they_ are super low-level.)

---

@jimmywarting Saying "`fetch` is a low-level API" also misses the point. It's sticking your head in the sand. People are increasingly using `fetch`, `isomorphic-fetch`, `node-fetch`, etc. in non-low-level places. They are using it as their only HTTP dependency for their apps, because bundle size is important in JavaScript. 

And because timeouts are not handled nicely in the spec, those polyfill libraries are incapable of adding `timeout` options themselves.

---

The options are:

1. **Make using timeouts with `fetch` more ergonomic.** That's what this proposal would do.

2. **Recommend that people do not use `fetch`, and always use a wrapper like `axios` instead.** And not just "those people who want timeouts", because as I've been saying that is everyone. You literally have to recommend that unless you are using `AbortController` and `AbortSignal`, you **should not** use `fetch`. That's extreme. And is not going to happen. It's already too popular, and for good reason. People want a nice standard library that helps them write good code.

3. **Do nothing and let the issue spread to `fetch`-using packages.** As more and more people (and libraries) use `fetch` under the covers, the indefinite hangs will become more and more common. You'll have to be wary of calling any `async` function that might delegate to `fetch` under the covers unless you know that it handles timeouts properly, and most of them won't have. Eventually people will start standardizing on a `timeout: number`-like property. (See `node-fetch`, which already has this issue.) But they won't all implement it the same. So the abstraction will leak further, because you'll need to know which timeout wrapper they've used.






-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/951#issuecomment-541447284

Received on Sunday, 13 October 2019 19:00:38 UTC