-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Immediately call onCancel
handlers when a promise is canceled
#24
Conversation
index.js
Outdated
@@ -46,7 +48,12 @@ class PCancelable { | |||
throw new Error('The `onCancel` handler was attached after the promise settled.'); | |||
} | |||
|
|||
this._cancelHandlers.push(handler); | |||
const fn = (...args) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you undo this change? A much better solution is to move the this._isCanceled = true;
before the if here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
index.js
Outdated
@@ -32,8 +32,10 @@ class PCancelable { | |||
this._reject = reject; | |||
|
|||
const onResolve = value => { | |||
this._isPending = false; | |||
resolve(value); | |||
if (this._isCanceled !== true || onCancel.shouldReject === false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (this._isCanceled !== true || onCancel.shouldReject === false) { | |
if (!this._isCanceled || !onCancel.shouldReject) { |
Add a test: test('throws immediately as soon as .cancel() is called', async t => {
const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
const timeout = setTimeout(() => {
resolve(true);
}, 10);
onCancel.shouldReject = true;
onCancel(() => {
clearTimeout(timeout);
resolve(false);
});
});
cancelablePromise.cancel();
await t.throwsAsync(cancelablePromise, {
message: 'Promise was canceled'
});
}); |
Is this still being worked on? I'm happy to help out if not. |
@ifiokjr That would be great! I don't think the author is active on GitHub anymore (I just guess by looking at his contributions chart). |
Friendly bump :) |
Oh wow, I totally forgot. Thanks for the ping.
…Sent from my iPhone
On Mar 7, 2021, at 6:58 AM, Sindre Sorhus ***@***.***> wrote:
Friendly bump :)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
onCancel
handlers when a promise is canceled.onCancel
handlers when a promise is canceled
Thanks :) |
Fixes #15