-
Notifications
You must be signed in to change notification settings - Fork 5
Description
This is an alternative idea based on a cancellation token. The aim with this was to produce a better idea from a "security" point of view that still had nice semantics for propagation etc.
The core idea is that you provide a CancellationToken which represents the ability to be notified of cancellation. You can only provide this when a promise is created, but if a promise wasn't provided with one when it was created, you can provide one later.
Terminology
- A
CancellationTokenis an object with methods for determining whether an operation should be cancelled - Exactly one
CancellationTokencan be associated with a promise. - Many
CancellationTokens can be registered with a promise.
Requirements
The arguments of a then method look like .then(onFulfilled, onRejected, onProgress, CancellationToken). For this spec, we are interested in CancellationToken.
CancellationToken
A CancellationToken is an object with the following two methods:
.isCancelled()must always returntrueorfalseonCancelled(cb)must callcbif Cancellation is triggered.
Direct Cancellation
If a promise has a CancellationToken associated with it (i.e. it was created via a call to .then that included a CancellationToken) then it is cancelled if and only if that CancellationToken is cancelled. If that CancellationToken is cancelled, it is immediately rejected.
Parent Cancellation
If a promise has no CancellationToken associated with it directly, it can be cancelled indirectly. All calls to .then that create a new promise must register that new promise's associated CancellationToken with the current promise.
This promise gets an internal CancellationToken associated with it that is cancelled whenever all registered CancellationTokens have been cancelled (providing at least one tick has gone past to allow for more tokens to be registered).
Child Cancellation
Any promise returned from a call to onFulfilled or onRejected should have the current promise's associated CancellationToken (even if it's internal via Parent Cancellation) registered with it via the call to .then necessary for assimilation.