utils

@cahil/utilsGlobalsasyncCancelablePromise

Class: CancelablePromise <T>

Creates a promise that can be canceled after starting. Canceling the promise does not stop it from executing but will cause it to reject with the value { isCanceled: true } once it finishes, regardless of outcome.


const promise = new CancelablePromise(res => setTimeout(res, 3000, 'I finished!'))

// Stop the cancelable promise from resolving
cancelablePromise.cancel()

cancelablePromise
  .then(result => console.log('Cancelable', result)) // Never fires, the promise will not resolve after being cancelled
  .catch(err => console.log('Cancelable', err)) // Resolves after 3000ms with the value `{ isCanceled: true }`

Type parameters

T: any

Hierarchy

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

+ new CancelablePromise(executor: function): CancelablePromise

Defined in src/async/CancelablePromise.ts:36

Parameters:

executor: function

▸ (resolve: function, reject: function): void

Parameters:

resolve: function

▸ (value: T): void

Parameters:

Name Type
value T

reject: function

▸ (err?: any): void

Parameters:

Name Type
err? any

Returns: CancelablePromise

Properties

Private canceled

canceled: boolean = false

Defined in src/async/CancelablePromise.ts:27


Protected promise

promise: Promise‹T›

Defined in src/async/CancelablePromise.ts:28


Static Promise

Promise: PromiseConstructor

Defined in node_modules/typescript/lib/lib.es2015.promise.d.ts:152

Accessors

__@toStringTag

get __@toStringTag(): string

Overrides void

Defined in src/async/CancelablePromise.ts:30

Returns: string


hasCanceled

get hasCanceled(): boolean

Defined in src/async/CancelablePromise.ts:34

Returns: boolean

Methods

cancel

cancel(): void

Defined in src/async/CancelablePromise.ts:47

Returns: void


catch

catch<TResult>(onrejected: function): *Promise‹T TResult›*

Overrides void

Defined in src/async/CancelablePromise.ts:58

Type parameters:

TResult

Parameters:

onrejected: function

▸ (reason: any): *TResult PromiseLike‹TResult›*

Parameters:

Name Type
reason any
Returns: *Promise‹T TResult›*

finally

finally(onfinally: function): Promise‹T›

Overrides void

Defined in src/async/CancelablePromise.ts:64

Parameters:

onfinally: function

▸ (): void

Returns: Promise‹T›


then

then<TResult1, TResult2>(onfulfilled: function, onrejected?: function): *Promise‹TResult1 TResult2›*

Overrides void

Defined in src/async/CancelablePromise.ts:51

Type parameters:

TResult1

TResult2

Parameters:

onfulfilled: function

▸ (value: T): *TResult1 PromiseLike‹TResult1›*

Parameters:

Name Type
value T

Optional onrejected: function

▸ (reason: any): *TResult2 PromiseLike‹TResult2›*

Parameters:

Name Type
reason any
Returns: *Promise‹TResult1 TResult2›*