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

> @ianstormtaylor Not to mention the example you gave seems kind of incomplete, and doesn't seem grounded in any real-world use cases.

That's not the point, of course it's a contrived example. My point is that (with your solution) people will eventually write code similar to that, and their code will now be broken, but they won't realize that it's broken. That's a big problem.

> It seems like you're not thinking in terms of the user's expectation.

I am well aware of the user's expectations. My point is that your proposal does not work, from a technical perspective.

> If you look at your example again, you'll notice that the solution I proposed has actually eliminated the uncertainty. The code you written, given the timeout option, is guaranteed to execute to completion, and not hang indefinitely. When the headers only are read, it will timeout for the headers. And in the if statement when the body is read, it will timeout for both the headers and body. Guaranteeing that its caller will receive control again no matter what happens to the HTTP response.

You have to look at it from the implementation's perspective. With your proposal, if the user does not access the body, then the timeout is cancelled. If the user does access the body, then the timeout is extended to cover the body.

But in my example, it uses `await checkSomething()`, which can take an arbitrary amount of time. So let's say that `await checkSomething()` takes 2 seconds. During that time, `fetch` *does not know* that you are going to later call `res.json()`. So it doesn't know whether it should cancel the timeout or not.

Remember, at this point `fetch` has already succeeded, and for the next 2 seconds you aren't calling `res.json()`. So `fetch` would have to wait for an indefinite amount of time, *just in case* you later call `res.json()`. That's the problem. It needs to be deterministic.

-- 
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-541391829

Received on Sunday, 13 October 2019 06:43:46 UTC