Class CancelablePromise<T>

Cancelable promises extend base promises and provide a cancel functionality than can be used to cancel the execution or task of the promise.

These type of promises can be used to prevent additional processing when the data is not longer required (e.g. HTTP request for data that is not longer necessary)

Type Parameters

  • T

Constructors

  • Type Parameters

    • T

    Parameters

    • executor: ((resolve, reject) => void)
        • (resolve, reject): void
        • Parameters

          • resolve: ((value) => void)
              • (value): void
              • Parameters

                • value: T | PromiseLike<T>

                Returns void

          • reject: ((reason?) => void)
              • (reason?): void
              • Parameters

                • Optional reason: any

                Returns void

          Returns void

    Returns CancelablePromise<T>

Properties

called: boolean = false

Flag set true when the resolve or reject method are called.

fulfilled: boolean = false

Flag to indicate if the promise has been fulfilled.

Promise has ben fulfilled when value/error is set.

onCancel: (() => void)

Type declaration

    • (): void
    • Returns void

onReject: ((error) => void)

Type declaration

    • (error): void
    • Parameters

      • error: any

      Returns void

onResolve: ((value) => void)

Type declaration

    • (value): void
    • Parameters

      • value: any

      Returns void

rejected: boolean = false

Flag to indicate if the promise was rejected.

Only set when the promise is fulfilled.

value: T

Output value of the promise.

Set with the output value if promise was fulfilled and not rejected.

Stores the error value if the promise was rejected.

Methods

  • Request to cancel the promise execution.

    Returns boolean

    True if the promise is canceled successfully, false otherwise.

  • Catch any error that occurs in the promise.

    Parameters

    • callback: ((error) => void)

      Method to catch errors.

        • (error): void
        • Parameters

          • error: any

          Returns void

    Returns CancelablePromise<T>

    Promise for chainning.

  • Executed after the promise is fulfilled.

    Parameters

    • callback: ((value) => void)

      Callback to receive the value.

        • (value): void
        • Parameters

          • value: any

          Returns void

    Returns CancelablePromise<T>

    Promise for chainning.

  • Wait for a set of promises to finish, creates a promise that waits for all running promises.

    If any of the promises fail it will reject altough some of them may have been completed with success.

    Parameters

    Returns CancelablePromise<any>

    Promise that will resolve when all of the running promises are fullfilled.

  • Create a rejected promise.

    Parameters

    • reason: any

      Reason to reject the promise.

    Returns CancelablePromise<any>

    Promise created with rejection reason.