Skip to content

editorGlobalsCancelToken

Class: CancelToken

A token that can be passed to called async child functions (or classes). The token is created with an error that will be thrown on cancellation to break the promise chain. Receivers of the token should call checkCancelled() after each await. When using the callback syntax for Promises, the reject callback should be registered with onCancel(). Functions that should be called on cancellation can be added with onCancel()

example const token = new CancelToken(new Error("cancelled")); // we can register things to be done on cancellation token.onCancel(() => console.log("cancellation started"));

const startAsyncProcess = async (cancelToken: CancelToken) => { await foo(); // Check the token after each await cancelToken.checkCancelled(); // If the process was cancelled, this will throw await bar(); cancelToken.checkCancelled(); }

const promise = startAsyncProcess(token);

// We may now cancel the token (e.g. when the user pressed a cancel button) // and inform the async process of the cancellation. // The promise should then reject with the error specified on creation. token.cancel();

example const token = new CancelToken(new Error("cancelled"));

const timedPromise = (cancelToken: CancelToken) => new Promise((resolve, reject) => { cancelToken.onCancel(reject); setTimeout(resolve, 1000) });

const promise = timedPromise(cancelToken) // if the token is cancelled within one second, the promise will reject with the error specified on creation token.cancel();

Hierarchy

  • CancelToken

Index

Constructors

Properties

Methods

Constructors

constructor

+ new CancelToken(cancelError?: Error): CancelToken

Defined in src/util/CancelToken.ts:43

create a new cancel token

Parameters:

Name Type Description
cancelError? Error the error instance that will be thrown on cancellation

Returns: CancelToken

Properties

Readonly cancelError

cancelError: Error

Defined in src/util/CancelToken.ts:41


Private cancelled

cancelled: boolean = false

Defined in src/util/CancelToken.ts:42


Private Readonly onCancelCallbacks

onCancelCallbacks: any[] = []

Defined in src/util/CancelToken.ts:43

Methods

cancel

cancel(): void

Defined in src/util/CancelToken.ts:64

call onCancel callbacks and throw cancelError on next checkCancelled

Returns: void


checkCancelled

checkCancelled(): void | never

Defined in src/util/CancelToken.ts:75

throw cancelError if cancelled

Returns: void | never


onCancel

onCancel(cancelFunction: function): void

Defined in src/util/CancelToken.ts:57

adds cancelFunction to the list of callbacks called on cancellation

Parameters:

cancelFunction: function

function that e.g. cancels children that do not use the token

▸ (error: Error): void

Parameters:

Name Type
error Error

Returns: void