simple-visual-novel
    Preparing search index...

    Class CancellablePromise<T>

    A promise that can be cancelled before completion.

    Extends the native Promise class to add cancellation support. When cancelled, the promise resolves (not rejects) and calls an optional cleanup callback.

    This is useful for animations and effects that need to be interruptible, allowing the final state to be shown immediately instead of waiting.

    CancellablePromise

    const promise = new CancellablePromise(
    (resolve) => {
    setTimeout(resolve, 5000); // Would take 5 seconds
    },
    () => {
    console.log("Cancelled! Cleaning up...");
    }
    );

    // Cancel after 1 second
    setTimeout(() => promise.cancel(), 1000);

    Type Parameters

    • T = void

      The type of value the promise resolves to (default: void)

    Hierarchy

    • Promise<T>
      • CancellablePromise
    Index

    Constructors

    • Creates a new CancellablePromise.

      Type Parameters

      • T = void

        The type of value the promise resolves to (default: void)

      Parameters

      • executor: (
            resolve: (value: T | PromiseLike<T>) => void,
            reject: (reason?: any) => void,
        ) => void

        The executor function, same as native Promise. Receives resolve and reject callbacks.

      • OptionalonCancel: () => void

        Optional cleanup callback invoked when cancel() is called. This runs before the promise resolves, allowing cleanup of timers, intervals, etc.

      Returns CancellablePromise<T>

    Properties

    "[toStringTag]": string
    "[species]": PromiseConstructor

    Accessors

    • get cancelled(): boolean

      Gets whether the promise has been cancelled.

      Returns boolean

      True if cancelled, false otherwise

    • set cancelled(value: boolean): void

      Sets whether the promise has been cancelled.

      Parameters

      • value: boolean

        The cancelled state

      Returns void

    Methods

    • Cancels the promise, invoking the cleanup callback and resolving immediately.

      This method:

      1. Marks the promise as cancelled
      2. Calls the onCancel callback (if provided) for cleanup
      3. Resolves the promise with the provided value (or undefined for void promises)

      Calling cancel() multiple times has no effect after the first call.

      Parameters

      • Optionalvalue: T

        Optional value to resolve the promise with. For void promises (default), this can be omitted.

      Returns void

    • Attaches a callback for only the rejection of the Promise.

      Type Parameters

      • TResult = never

      Parameters

      • Optionalonrejected: ((reason: any) => TResult | PromiseLike<TResult>) | null

        The callback to execute when the Promise is rejected.

      Returns Promise<T | TResult>

      A Promise for the completion of the callback.

    • Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.

      Parameters

      • Optionalonfinally: (() => void) | null

        The callback to execute when the Promise is settled (fulfilled or rejected).

      Returns Promise<T>

      A Promise for the completion of the callback.

    • Attaches callbacks for the resolution and/or rejection of the Promise.

      Type Parameters

      • TResult1 = T
      • TResult2 = never

      Parameters

      • Optionalonfulfilled: ((value: T) => TResult1 | PromiseLike<TResult1>) | null

        The callback to execute when the Promise is resolved.

      • Optionalonrejected: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null

        The callback to execute when the Promise is rejected.

      Returns Promise<TResult1 | TResult2>

      A Promise for the completion of which ever callback is executed.

    • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

      Type Parameters

      • T

      Parameters

      • values: Iterable<T | PromiseLike<T>>

        An iterable of Promises.

      Returns Promise<Awaited<T>[]>

      A new Promise.

    • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

        An array of Promises.

      Returns Promise<{ -readonly [P in string | number | symbol]: Awaited<T[P<P>]> }>

      A new Promise.

    • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

        An array of Promises.

      Returns Promise<
          {
              -readonly [P in string
              | number
              | symbol]: PromiseSettledResult<Awaited<T[P<P>]>>
          },
      >

      A new Promise.

    • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.

      Type Parameters

      • T

      Parameters

      • values: Iterable<T | PromiseLike<T>>

        An array of Promises.

      Returns Promise<PromiseSettledResult<Awaited<T>>[]>

      A new Promise.

    • The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

        An array or iterable of Promises.

      Returns Promise<Awaited<T[number]>>

      A new Promise.

    • The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.

      Type Parameters

      • T

      Parameters

      • values: Iterable<T | PromiseLike<T>>

        An array or iterable of Promises.

      Returns Promise<Awaited<T>>

      A new Promise.

    • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

      Type Parameters

      • T

      Parameters

      • values: Iterable<T | PromiseLike<T>>

        An iterable of Promises.

      Returns Promise<Awaited<T>>

      A new Promise.

    • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

        An array of Promises.

      Returns Promise<Awaited<T[number]>>

      A new Promise.

    • Creates a new rejected promise for the provided reason.

      Type Parameters

      • T = never

      Parameters

      • Optionalreason: any

        The reason the promise was rejected.

      Returns Promise<T>

      A new rejected Promise.

    • Creates a new resolved promise.

      Returns Promise<void>

      A resolved promise.

    • Creates a new resolved promise for the provided value.

      Type Parameters

      • T

      Parameters

      • value: T

        A promise.

      Returns Promise<Awaited<T>>

      A promise whose internal state matches the provided promise.

    • Creates a new resolved promise for the provided value.

      Type Parameters

      • T

      Parameters

      • value: T | PromiseLike<T>

        A promise.

      Returns Promise<Awaited<T>>

      A promise whose internal state matches the provided promise.

    • Takes a callback of any kind (returns or throws, synchronously or asynchronously) and wraps its result in a Promise.

      Type Parameters

      • T
      • U extends unknown[]

      Parameters

      • callbackFn: (...args: U) => T | PromiseLike<T>

        A function that is called synchronously. It can do anything: either return a value, throw an error, or return a promise.

      • ...args: U

        Additional arguments, that will be passed to the callback.

      Returns Promise<Awaited<T>>

      A Promise that is:

      • Already fulfilled, if the callback synchronously returns a value.
      • Already rejected, if the callback synchronously throws an error.
      • Asynchronously fulfilled or rejected, if the callback returns a promise.
    • Creates a new Promise and returns it in an object, along with its resolve and reject functions.

      Type Parameters

      • T

      Returns PromiseWithResolvers<T>

      An object with the properties promise, resolve, and reject.

      const { promise, resolve, reject } = Promise.withResolvers<T>();