-
-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[RFC] API for canceling HTTP requests #333
Comments
Option 3APIimport axios from 'axios'
let cancelRequest
let promise = new Promise((resolve) => {
cancelRequest = resolve
});
window.addEventListener('beforeunload', (e) => {
cancelRequest()
})
axios.get('/user', {
timeout: promise
}) Notes
ImplementationThe implementation for this is simply checking |
Abort on retry can be even more important than abort by itself. For example the most common use case for aborting XHR requests is when you are doing an autocomplete logic, we should discuss that feature as well. |
I just saw that TC39 is discussing a proposal for Cancelable Promises |
@mzabriskie Yes, I'm aware of that proposal. It consists of two parts (slide 23):
I don't think we can use cancelable promises in axios API before they are implemented in the browsers (I may be wrong). But I do think that we can use cancellation tokens to request cancellation. That is what Option 2 is about. I think we should align the cancellation token API to match the one described in the proposal though. As for the third state, I would suggest to reject promises with a special error (e.g. CancellationError) for now. Once cancellable tokens are implemented, we can update axios to cancel the promise instead of rejecting it. This is not going to happen soon, so I don't think we should wait for it. Thoughts? |
Option 1 seems good enough for me. Also liked @herrstucki's approach to the issue. |
Is there a timeline for the abort API to be finalized and implemented? IMO, it is a good idea to have the API inline with the Cancelable Promise spec. |
I think it is possible to implement any option as a custom adapter (based on XHR) and then try it in a real project. |
I think Option 1 is good idea . @mzabriskie |
I tried to use |
I also second Option 2, it's more future proof. @nickuraltsev any ETA? In the meantime, I might just do what @just-boris did or deal with a fork... |
I like the idea of cancellation tokens as @just-boris is doing - this is also how it's done in .NET. Basically make a cancellation token and pass it in, and when |
Option 2 is really awesome...! |
I think that while promise calcellation spec is not finalized, this feature will not be implemented in core. To gracefully adopt cancellation feature, it would be better to use this as plugin, like I did. If anybody interested, I can publish axios-cancel on NPM for convinience. |
I believe this feature should be implemented in core. There is no way to build it as a plugin without re-implementing axios adapters as it's done in axios-cancel which seems to be more a fork rather than a plugin. Here is my suggestion:
I think that the short-term solution doesn't need to be perfect. The goal is to make it possible to cancel requests and handle cancellations. Once it's in place, users will have a choice of using the axios API as is or build another API on top of it. Now, there are no options except re-implementing the adapter as @just-boris did. @mzabriskie If you agree with this approach, I can create a PR. Please let me know what you think. |
I don't think that creating offshoot adapter implementations is the correct approach. This will be difficult to maintain and keep synchronized with the core adapters. There will also need to be an offshoot maintained for both node's http as well as browser's xhr. I agree with having a short-term and long-term plan. I support @nickuraltsev's proposal above. |
Great, I'll submit a PR soon. |
@nickuraltsev what's current progress status? |
I vote for option 1 or option 2 |
Any news? |
Just a heads up that I'm actively working on this feature. I'm hoping it will be ready in a couple of days. |
I've just submitted a PR |
Any news on when this will be released in an official version? |
Fixed via #452 |
FYI: Promise Cancellation Is Dead — Long Live Promise Cancellation! |
Overview
I would like to revive the discussion about canceling HTTP requests (#50) as there is clearly a demand for this feature. I suggest considering the following options. /cc @mzabriskie
Option 1
API
Implementation
PR #330
Option 2
API
Notes
Promise
is rejected with aCancellationError
. This allows the caller to distinguish between a cancellation and a failure.Cancellation
object can be used to cancel multiple different requests.Implementation
I created a separate library that can be used as a base for implementing request cancellation in axios: https://github.com/nickuraltsev/cancel. (The example on the README.md shows how to implement a simple HTTP client that supports cancellation.) We can copy all the code from this library to axios or add this library as a dependency.
The text was updated successfully, but these errors were encountered: