-
Notifications
You must be signed in to change notification settings - Fork 294
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
Consider adding a timeout parameter to the AbortController constructor #1110
Comments
One unfortunate side-effect is that new option parameter are difficult to feature detect, vs. a static function property, which would suggest devs would need to fallback to UA sniffing to determine whether or not they have to handle timeout or have the option of a constructor parameter. |
Ah good point. I wonder here though if using EnforceRange solves that? If |
A timeout of 0 could return an instantly aborted controller, so one could do feature detection: const a = new AbortController({ timeout: 0 })
const supported = a.signal.aborted Failing that one can detect support with a getter: let supported = false
const o = {}
Object.defineProperty(o, 'timeout', { get() { supported = true }})
const a = new AbortController(o) |
Yeah, @keithamus is correct. Dictionary members can be detected quite well in most cases. There's another thread on timeouts (see the issues with the same label) and one question I have is whether this should perhaps be a subclass of sorts as you probably also want to give the controller access to adjusting the timeout value, canceling it, and perhaps resetting it. Maybe it's okay to put all that on |
I think this proposal is a bad idea and would be poor API design. The constructor of an object should vend its fundamental capabilities. It should not provide a syntactic shortcut for saving an extra line of setup, by connecting the constructed object to a totally different part of the web platform. (In this case, the event loop and timer subsystem.) Furthermore, I don't think we have evidence that the use case in question (roughly, creating an I hope we keep the |
I’ve needed to compose timeout-related signals and other signals many times. In some cases this did mean just one timeout + its creator had controller access, but very often the relationships were more complex than that (or became more complex than that later) — e.g. branching where a signal from above (controller not passed down) could end up being one of multiple “abort factors” for its “descendants”. We eventually ended up with a set of compositional utils for merging signals (and sometimes, but less frequently, controllers). These often had clear symmetry with static Promise methods. With a small library of those utils, managing AC/AS eventually became a breeze ... but it took time to land on the right mental model & understand what was really called for. This learning curve was reminiscent to me of the period years ago when lots of folks were fuzzy on how to use Promise effectively & tended to flip the resolvers/promise relationship around. If the Promise constructor had introduced a timeout argument then, I don’t think it would have helped the situation. In that analogy, controller.abort is more or less (The absence of |
I ran into this issue today. I definitely agree that the I was actually looking to use To summarize: because |
I think @bathos' comment is addressed with the addition of @adixon-adobe I guess there's a question of whether there's room for a static that does something like the following function controllerWithTimeout(timeout) {
const controller = new AbortController();
const timeoutSignal = AbortSignal.timeout(timeout);
const signal = AbortSignal.any([controller.signal, timeoutSignal]);
return { controller, signal };
} and is perhaps a bit cleaner/magical (by flattening |
@annevk I still found myself wanting to pass in a parameter or custom So I still think there's likely to be some frustration around this. And that doesn't necessarily mean it's worth prioritizing -- just want to make sure my feedback/experience is clear. |
Feedback from a TAG design review of
AbortSignal.timeout()
suggested addingtimeout
as a parameter to theAbortController
constructor to improve ergonomics for the case where both a timeout and a controller is needed:I agree this would improve ergonomics for that case (even if we add
AbortSignal.any()
), and it still leaves open the possibility of adding timeout manipulation later (e.g. #1039). FWIW, as I mentioned in the TAG issue, there is prior art on other platforms for doing something like this, e.g. .NET'sCancellationTokenSource
has a constructor timeout parameter and Go'sContext
has a WithTimeout function, both of which have similar behavior to what's being proposed here.Thoughts? Does this seem worth adding, or would it be better to consider this along with timeout manipulation and design them together?
The text was updated successfully, but these errors were encountered: