Skip to content
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

cancel parameter #4

Open
bergus opened this issue Aug 17, 2016 · 1 comment
Open

cancel parameter #4

bergus opened this issue Aug 17, 2016 · 1 comment

Comments

@bergus
Copy link

bergus commented Aug 17, 2016

Your spec says

The new cancel parameter is a function which may be invoked from within the executor to cancel the promise, passing along a cancellation detail

What does it do? Why would it need to be invoked? What's the "cancellation detail", doesn't that conflict with the notion of staying forever pending?

And wouldn't it be easier to always install cancel directly on the canceler?

@rtm
Copy link
Owner

rtm commented Aug 22, 2016

What does resolve do? It resolves the promise. What does reject do? It rejects the promise. What does cancel do? It cancels the promise.

The notion is that just as the executor embodies the logic for when and if to resolve or reject the promise, the executor also embodies the logic for when and if to cancel the promise. Just as resolve and reject do nothing it the promise is already settled, cancel does nothing if the promise is already canceled or settled.

Asking why it would need to be invoked is analogous to asking why resolve or reject would need to be invoked. Just as resolve would need to be invoked when you want to resolve the promise, cancel would need to be invoked if you wanted to cancel the promise from within the executor logic. The precise reason is up to the application.

Although "canceled" in the model I'm working on here is indeed equivalent to pending in the sense that then handlers would never be invoked, it is nevertheless a distinct sub-state of pending, and as such it is useful to record and pass (to the .onCancel handler) some information about why the promise was canceled, and in order to avoid confusion with "reason", which is usually associated with rejection, I've just decided to call this "detail" instead.

The canceler parameter to the Promise constructor is actually a kind of syntactic sugar for adding your own call to cancel inside the executor. What it does, if you have the time to look at the code, is to build an executor around the underlying executor, which cancels (by calling cancel) if the canceler promise fulfills, with the twist that the underlying executor is wrapped so as wait an extra tick for the canceler to resolve, and is skipped altogether if it has.

Hope this is relatively clear.

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

No branches or pull requests

2 participants