Skip to content

Cancellation Token #8

@ForbesLindesay

Description

@ForbesLindesay

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

  1. A CancellationToken is an object with methods for determining whether an operation should be cancelled
  2. Exactly one CancellationToken can be associated with a promise.
  3. 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:

  1. .isCancelled() must always return true or false
  2. onCancelled(cb) must call cb if 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions