Open Design

Function createCancelToken

createCancelToken
{ empty: CancelToken; fromSignal: (signal: AbortSignal) => { dispose: () => void; token: CancelToken } }

Creates a cancellation token which can be used for aborting asynchronous operations of the SDK.

Most asynchronous methods accept a cancellation token (the returned token). The same cancellation token can be used for multiple sequential as well as parallel operations. Finished operations no longer react to cancellations.

This mechanism is analogous to the standard AbortSignal/AbortController API with the difference that a cancellation reason can be specified. The created tokens are also somehow compatible with the standard API by exposing the standard AbortSignal as token.signal, just as it is possible to create a CancelToken from an AbortSignal via createCancelToken.fromSignal().

Example:

const controller = createCancelToken()

sdk.fetchDesignById('<ID>', { cancelToken: controller.token })
  .then((design) => {
    doStuffWithDesign(design)
    controller.dispose()
  })
  .catch((err) => {
    if (err.code !== 'OperationCancelled') { throw err }
  })

setTimeout(() => {
  controller.cancel('Timed out.')
}, 2000)

Keys:

A cancellation token which never gets cancelled.

This token can be used for logic simplification in place of actual working tokens as a default (i.e. cancelToken || null to avoid the need for token?.throwIfCancelled()).

fromSignal: (signal: AbortSignal) => { dispose: () => void; token: CancelToken }

Wraps an existing standard AbortSignal in a new cancellation token which can be used with the SDK.