Skip to content
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

Use AbortController to abort fetches and more. #4170

Open
chrisbobbe opened this issue Jun 23, 2020 · 3 comments
Open

Use AbortController to abort fetches and more. #4170

chrisbobbe opened this issue Jun 23, 2020 · 3 comments
Labels
a-data-sync Zulip's event system, event queues, staleness/liveness a-multi-org

Comments

@chrisbobbe
Copy link
Contributor

See discussion (thanks @andersk for pointing this out, and @gnprice for nudging me to look closer!).

With facebook/react-native#24419, React Native (v0.60) gained a polyfill for the AbortController Web API; see the MDN doc.

The MDN doc leads with this:

The AbortController interface represents a controller object that allows you to abort one or more Web requests as and when desired.

But it's not constrained to only work with "Web requests"; Anders links to a Google developer doc that says this:

It's deliberately generic so it can be used by other web standards and JavaScript libraries.

This sounds like it's not an abomination to use it for more basic control-flow needs that have nothing to do with fetch or its friends. In particular, we can use this mechanism to break out of the while (true) loop in tryUntilSuccessful (soon to be renamed to tryFetch in, I think, #4166), when we want to abort. (In that PR, that particular need is answered with a local stateful boolean variable—it's nice to know there's a standard, well-documented way of doing it.)

And, of course, we'll actually be able to abort fetch requests.

Fingers are crossed that this React Native polyfill works; there's some history of RN-shipped polyfills being buggy.

@chrisbobbe
Copy link
Contributor Author

I think the article linked from this tweet from Dan Abramov is probably worth reading, at least in part, if only just because it's Dan Abramov. I haven't read it yet. To quote the tweet:

I should probably clarify the technical aspect that I don’t earnestly believe Promises should be cancelable. It doesn’t work with the read-only design. If you’re interested in reasonable modern approaches to cancellation, this is a good read. https://vorpus.org/blog/timeouts-and-cancellation-for-humans/

@chrisbobbe
Copy link
Contributor Author

Related: #3881

@chrisbobbe
Copy link
Contributor Author

I've just filed #4659 for an implementation plan to expose easy methods for canceling fetches; after that work is done, we'll just have to call those methods in the right places. (tryFetch after #4193 may be such a place.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-data-sync Zulip's event system, event queues, staleness/liveness a-multi-org
Projects
None yet
Development

No branches or pull requests

2 participants