-
Notifications
You must be signed in to change notification settings - Fork 73
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
Make HTTP requests cancelable #74
Comments
Afraid not, and looking at the current implementation I'm not sure how it would be supported. Promise cancellation is still up in the air afaik. |
This might not be what you want, but you can pass a promise into |
I am renaming this issue, from “http.request.abort()” to “Make HTTP requests cancelable”. |
Yes, actually, it was wrong question ( I've seen there is no way to abort request now =), the right one is — how it can be gracefully implemented? |
@surr-name That is approximately what would be necessary, except that we would have to support the promise cancel method at the Q level so it would not be a hack. That unfortunately implies a bunch of other things, see kriskowal/q#61. |
Pardon, kriskowal/q#64 |
In my opinion it is not a global problem of promises, it is a local problem of applying promises to http.request. In any case, how one can access through |
Again, ideally,
|
Speaking with @erights, it seems that state-of-the-art for this kind of problem is to pass a promise as an optional argument. Rejecting that promise would be a signal that the subscriber would like the operation to be cancelled. |
If we implement cancelability in Q-IO, we will accept a "canceled" promise as an argument. If we rebuilt something like Q-IO on top of https://github.com/kriskowal/gtor, we would use a Task instead of a Promise, and allow the user to coerce said task to a promise if they chose to attenuate cancelability. |
From the gtor document it seems you still agree about cancellation promises However, the end of that document suggests that until WeakRefs are On Thu, Sep 11, 2014 at 5:27 PM, Kris Kowal notifications@github.com
Text by me above is hereby placed in the public domain Cheers, |
I am academically interested in exploring the paradigm of Tasks and Promises side-by-side with measured understanding of the limitations of both (GToR) but pragmatically committed to using promises alone (Q, Q-IO, Q-Connection, id est Q*, ECMAScript). |
That's fine of course. But what is your point about WeakRefs? This answer On Thu, Sep 11, 2014 at 8:15 PM, Kris Kowal notifications@github.com
Text by me above is hereby placed in the public domain Cheers, |
I really can't imagine a place of cancelability at Promise's paradigm. Can you draft your vision of it? there is a lot of questions about it:
And again, don't you think that aborting HTTP requests is not a problem at Promise's level? May be it can to be a decision to add possibility to create a special object of additional methods for particular promise at deferred's level? |
An example to illustrate my last paragraph: function getPromise () {
var D = Q.defer();
// ...
D.promise._extra = {
doSomeExtraAction : function () {
// do something, for example:
D.reject();
// or abort an HTTP request,
// or anything else
}
};
return D.promise;
}
var promise = getPromise();
promise
.then(function(){
// ...
})
.fail(function(){
// ...
});
setTimeout(function(){
if ( something ) promise._extra.doSomeExtraAction();
}, 5000); |
Sorry, if it is wrong place for discussing cancelable promises, but don't you think this way has no direct collisions with Promise A+ spec, unlike cancelablement has? It can resolve the problem with aborting HTTP requests, and a lot of others. |
@surr-name, sorry for the distraction about cancelable promises. Exposing cancelation was my intuition for the right API for the feature you are suggesting. I am suggesting now that this intuition had fundamental flaws and we will not explore that direction here. There is a one way communication channel from a resolver to a promise. To abort an HTTP request is to communicate in the other direction, from the consumer to the producer. Mark Miller is suggesting that we establish an explicit channel in that direction, giving the consumer a resolver and the producer retaining a promise. This is slightly different than your suggestion of using a deferred, but in the same spirit. The interface might look like: var cancellation = Q.defer();
var responsePromise = HTTP.request({url, method, cancelled: cancellation.promise});
// To communicate impatience or disinterest:
cancellation.reject(new Error("Client no longer interested in HTTP response"); Q-IO’s HTTP module would observe the cancellation promise if one is provided and abort if it settles. Mark had some suggestions about fail-only promises which we might explore, but in principle, the cancellation promise should only be rejected and that rejection would propagate back to the response promise. And this would be a pattern that we would expose throughout Q-IO for cancellation in general. I will leave the academic discussion of the alternative approach of using Task to the GToR project and discourage continuing that discussion here. |
sounds good, but Let me illustrate it with stupid example: function giveMeAnApple () {
var D = Q.defer();
// ...
// ok, I've no apples for the moment,
// but I'll try to buy one for you
var promiseForAnApple = D.promise;
// It'll be red,
var color = 'red';
// but untill I've bought it,
// you can say wich color you prefer
promiseForAnApple._extra = {
iWantColor : function ( preferedColor ) {
if ( promiseForAnApple.isPending ) color = preferedColor;
}
};
return promiseForAnApple;
}
// ...
var promise = giveMeAnApple();
promise
.then(transferAppleToChild, cryAboutInjustice);
askChildForColor()
.then(promise._extra.iWantColor); Do you see any weakness of this pattern? |
The example is really stupid and says nothing about HTTP request, but shows a flexibility. What about the requests: |
Although I do not understand what this example illustrates, I can say without hesitation that we will do nothing that depends on promises being mutable. Promises are designed specifically such that they can be frozen and distributed to mutually suspicious consumers without introducing a covert communication channel or opportunity to interfere between them. |
You're right about the Promises, but the way doesn't affects Promises, it is beside, it is about wide communication channel between a consumer and a producer of some task.
|
See #124 for resolution. |
There is not presently a way to abort an outstanding HTTP request. This would require a mechanism for canceling a promise, which is not yet supported by Q.
The text was updated successfully, but these errors were encountered: