Skip to content

Immediately call onCancel handlers when a promise is canceled#24

Merged
sindresorhus merged 2 commits intosindresorhus:mainfrom
jonschlinkert:master
Mar 10, 2021
Merged

Immediately call onCancel handlers when a promise is canceled#24
sindresorhus merged 2 commits intosindresorhus:mainfrom
jonschlinkert:master

Conversation

@jonschlinkert
Copy link
Copy Markdown
Contributor

Fixes #15

index.js Outdated
}

this._cancelHandlers.push(handler);
const fn = (...args) => {
Copy link
Copy Markdown
Contributor

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:

https://github.com/jonschlinkert/p-cancelable/blob/deaeee112adc3d105aaec2074de971af5dd48c3a/index.js#L90-L100

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index.js Outdated
const onResolve = value => {
this._isPending = false;
resolve(value);
if (this._isCanceled !== true || onCancel.shouldReject === false) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (this._isCanceled !== true || onCancel.shouldReject === false) {
if (!this._isCanceled || !onCancel.shouldReject) {

@szmarczak
Copy link
Copy Markdown
Contributor

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'
	});
});

@ifiokjr
Copy link
Copy Markdown

ifiokjr commented Sep 4, 2020

Is this still being worked on?

I'm happy to help out if not.

@szmarczak
Copy link
Copy Markdown
Contributor

@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).

Base automatically changed from master to main January 24, 2021 06:15
@sindresorhus
Copy link
Copy Markdown
Owner

Friendly bump :)

@jonschlinkert
Copy link
Copy Markdown
Contributor Author

jonschlinkert commented Mar 8, 2021 via email

jonschlinkert added a commit to jonschlinkert/p-cancelable that referenced this pull request Mar 9, 2021
@sindresorhus sindresorhus changed the title Immediately call onCancel handlers when a promise is canceled. Immediately call onCancel handlers when a promise is canceled Mar 10, 2021
@sindresorhus sindresorhus merged commit d292dcc into sindresorhus:main Mar 10, 2021
@sindresorhus
Copy link
Copy Markdown
Owner

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

onCancel handlers should be called immediately if the promise was cancelled already

4 participants