-
Notifications
You must be signed in to change notification settings - Fork 298
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 cancellation support #123
Add cancellation support #123
Conversation
Awesome, thank you @not-an-aardvark ! I am in the middle of doing a major refactoring for request-promise and this makes my job easier. However, please be a little patient until I release the next version. |
Can't wait for new version with this feature. Do you know, @analog-nico, when you will approximately finish refactor? :-) |
Awesome timing @horejsek ! 😄 I just release the new |
Nice! 👍 |
Can i use cancel promise in the latest version? |
An update on this PR: I've been trying to figure out a way to reimplement cancellation semantics in request-promise 4, but at the moment I'm unsure of whether this is possible without adding a hook to the The issue is that while bluebird allows a cancellation listener to be added from the promise constructor (e.g. As an alternative, I considered something like this in request.Request.prototype.cancel = function() {
this._rp_promise.cancel();
this.abort();
}; While this aborts the request if var promise = request.get('http://example.com').then(function() {
// handle response
});
promise.cancel(); // does not abort the request So I think it is necessary to add a hook to var configure = require('@request/promise-core/configure/request2');
configure({
request: request,
PromiseImpl: Bluebird,
expose: [
'then',
'catch',
'finally',
'cancel',
'promise'
],
constructorOverride: function (resolve, reject, onCancel) {
this._rp_resolve = resolve;
this._rp_reject = reject;
onCancel(function() {
this.abort();
});
}
}); ...but this solution seems a bit inelegant, since @analog-nico, do you have any suggestions for how to handle this issue? |
Hi @not-an-aardvark , thanks for putting your thoughts into this!!! Your
self._rp_promise = new PromiseImpl(function (resolve, reject) {
self._rp_resolve = resolve;
self._rp_reject = reject;
+ if (options.constructorMixin) {
+ options.constructorMixin.apply(self, arguments);
+ }
}); And then var configure = require('@request/promise-core/configure/request2');
configure({
request: request,
PromiseImpl: Bluebird,
expose: [
'then',
'catch',
'finally',
'cancel',
'promise'
],
constructorMixin: function (resolve, reject, onCancel) {
var self = this;
onCancel(function() {
self.abort();
});
}
}); Additionally, I see the following todos:
|
@nhducit We are working on it. I'll let everyone know in this PR once a new version is published that supports cancellation. |
Thanks,
Bluebird 3.4.1 has a getNewLibraryCopy function, which creates a pristine version of Bluebird to avoid modifying global state like you described. |
Alright. I am on it. |
418093f
to
6b9fd01
Compare
I pushed an updated version of this PR which should work after |
Awesome, thank you! |
@not-an-aardvark in case you wondered: I just added a missing piece I forgot in |
Hey guys, I just published version 4.1.0 which exposes the Thank you very much @not-an-aardvark for making this happen! |
thank you for your update |
Fixes #113
This change adds bluebird's
.cancel
function to all request objects from request-promise. When the.cancel
function is called, the Promise is cancelled, and the corresponding request is aborted.