-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add timeout option #20
Comments
Ideally we would have a design for cancelable promises that fetch could use. But unfortunately there are still four or five competing proposals thrown out and every time a web developer asks me "why can't we do cancelable promises?" and I look into exactly what they're asking for I end up adding a new proposal to that list. It would take some concerted effort to try to find something good. In the meantime I think a sensible cancelation strategy is to reject the promise with some sort of error. The actual cancel() method if it exists should not be on the returned promise though, at least not until we figure out cancelable promises. That leaves a couple options I can see:
As for the error itself we could future-proof there slightly better. If we create a new |
This doesn't feel particularly useful… the cases where you would want a fetch to timeout would be when the network conditions were not good and you weren't getting anything back… Given timeout is such a common use case, even if promises had a concept of being cancelled providing a |
To me it seems that having API to cancel started fetch is completely separate from request timeouts. Mechanism used for cancellation on timeout can be completely private (for now). |
@pornel yes, perhaps. If we expose termination I would expect it to be on |
@matthew-andrews baby steps. It'll come. |
What are the reasons that rejection of an ordinary promise would be an unacceptable solution?@domenic |
How do you envision that would work? |
Assuming that fetch creates the promise, then can't it just have a setTimeout* within the executor function which calls reject? Why is that not feasible?
- @domenic Why is that not good enough to run with? *or equivalent in whichever language the browser implements it in |
If this is spec'd, please also define behavior for timeouts occurring after headers are received, but before the end of the content body. |
same question here, I wanna abort() request. I think what we need is in my opinion will like this. var req = new Request(url, option);
req.fetch().then((res) => {
console.log(res.status);
}).catch((err) => {
console.error(err); // this will also happen in `abort()` request
});
// timeout a request
setTimeout(function() {
req.abort(); // reject the fetching process
}, 1000); |
I agree that we want something like that. It's a bit unclear to me still what the exact semantics should be. |
As one possible option, |
The semantics are still unclear. Does the timeout run until the promise returns? Does the timeout terminate the underlying stream somehow once the promise has returned? |
@matthew-andrews I think timeout should be Hi-level API on @annevk I think node.js http module will help us including make res as streams for getting progress event. |
There's an interesting difference between "deadline" and "timeout". From
The "deadline" option is implementable using |
that's interesting but is that scope of fetching ? in fetch context, I think in application, that will use for terminate the process gracefully, If you wanna do something like that, raw-socket API may works. |
@Jxck Yes, I think timeout is in scope of fetching, and that's an important feature. In the FT app we've had to add timeouts to every network request, because we've learned that on some networks requests just never finish, and that can make the app unusable. Now we're treating network requests without a timeout as a bug. I'm not opposed to having an API to abort requests. I think these are largely independent problems: it's possible to have timeouts without a public abort API, and it's possible to have only abort API and leave timeout implementation to the user. I think having both would be the best. If the decision was to only have an abort API instead of support for timeout, then that would be workable, but not ideal — I'd always have to wrap fetch in |
ah ok you talking about adding a option doesn't breaking changes of in some browser start to implements fetch(), so I think we need to start discussion about |
The problem with (There is also some complication with Would need to think a bit about how we can change some of these characteristics to make it all fit better. |
It depends how high of an API fetch should be. If it's a low-level request - we don't need abort. We'll wrap it in a higher level promise API and that's it. If on the other hand it's high-level and we want to abort, it should be instantiated and work like a resource so: var someResource = new Fetch(); someResource.abort(); The first option imho is better. |
Discussion for Leaving this open for the timeout idea from @pornel which seems like a rather interesting primitive to expose, network stacks allowing. |
doing fetch with just one function is difficult to expand with adding api in future. in current interface, we do all thing with
the problem is current I heard that chrome are going to export fetch to window in M42 (mid April) |
What is wrong with |
Also, I would appreciate it if you could move such a discussion to a new issue. Or perhaps we should move timeout to a new issue as this is getting rather cluttered. |
There's a potentially different kind of timeout here: https://aws.amazon.com/blogs/aws/elb-idle-timeout-control/ e.g. with an idle timeout of 15 seconds:
I don't think it is always the intention for a timeout to only consider the time until the first byte I don't think it is always the intention for a timeout to consider full receipt of the headers and body But each of these ways of defining timeouts is useful in different situations |
@Mouvedia As the spec clearly shows (in step 14), XHR will wait for both the headers and the body to complete. The timeout for XHR includes the body. Please read the spec more carefully.
The XHR is not the same as @jokeyrhyme That's a good point, that complicates the timeout even more. Which is another argument for having this in user-land, so that way the user has full control, rather than adding in several options to |
@Mouvedia Yes, you won't receive another chunk, but the promise of |
@jokeyrhyme I doubt whether this can be detected in JavaScript. Now that the body is a stream, is browser allowed to buffer the response? Is browser supposed to trigger any JavaScript event as soon as it receive a single byte from server? |
This comment has been minimized.
This comment has been minimized.
# This is the 1st commit message: # This is a combination of 23 commits. # This is the 1st commit message: Integrate CORP and COEP This is part of the introduction of COEP (whatwg/html#5454). The CORP check now takes COEP into account. Also, responses coming from service workers are checked. # This is the commit message #2: Update fetch.bs Co-authored-by: Domenic Denicola <[email protected]> # This is the commit message #3: Update fetch.bs Co-authored-by: Domenic Denicola <[email protected]> # This is the commit message #4: fix # This is the commit message #5: fix # This is the commit message #6: fix # This is the commit message #7: fix # This is the commit message #8: fix # This is the commit message #9: fix # This is the commit message #10: fix # This is the commit message #11: fix # This is the commit message #12: fix # This is the commit message #13: fix # This is the commit message #14: fix # This is the commit message #15: fix # This is the commit message #16: fix # This is the commit message #17: fix # This is the commit message #18: Update fetch.bs Co-authored-by: Anne van Kesteren <[email protected]> # This is the commit message #19: Update fetch.bs Co-authored-by: Anne van Kesteren <[email protected]> # This is the commit message #20: fix # This is the commit message #21: fix # This is the commit message #22: fix # This is the commit message #23: fix # This is the commit message #2: fix
I noticed some misunderstanding about We're considering adding timeout support to AbortSignal: whatwg/dom#951. What I'm wondering is whether people need to distinguish between the different reasons a fetch might have aborted or whether that's immaterial. |
What's annoying is non-parity with XHR: |
That doesn't make sense. You pass a signal to |
Are you talking about the specification or the implementations? Do you have tests for that? Anyway what I meant is, please try to strive for parity with XHR. We don't want more gotchas: the specification needs to be explicit. Ill give you another example, in XHR abortion behaviour on page quit wasn't clear at first. |
I'm not sure I follow. There are open issues around document unloading, but they affect XMLHttpRequest and |
From the point of view of developers, adding
Are you adding |
It's on top of signal, so deadline, which is also what XMLHttpRequest has. However, we might add signals to reading from a stream, which would allow for the former. |
Dunno what the others think but it would be better if we could stay consistent regarding terminology. |
How can I add timeout to request now? |
Should this be considered fixed as we now have |
// Fetch the URL, cancelling after 8 seconds
fetch(url, { signal: AbortSignal.timeout(8000) }); Apparently available in Firefox 100 and Chrome 103 beta!! |
And in Firefox 100 😉 |
Thanks, wasn't aware about this, updated my post above! |
Most ajax libraries and xhr itself offer a convenient way to set a timeout for a request. Adding
timeout
to the properties accepted in the options object therefore seems like a sensible addition to fetch.JakeChampion/fetch#68
The text was updated successfully, but these errors were encountered: