Tweakr
    Preparing search index...

    Function withCancel

    • Wraps a promise with a cancelable interface.

      The returned object contains a promise and a cancel method. If cancel is called before the promise settles, the promise rejects with a "Cancelled" error.

      Type Parameters

      • T

        The type of the promise’s resolved value.

      Parameters

      • promise: Promise<T>

        The promise to wrap with cancellation capability.

      Returns { cancel: () => boolean; promise: Promise<T> }

      An object with the wrapped promise and a cancel function.

      const { promise, cancel } = withCancel(fetch("/api/data"));

      setTimeout(() => cancel(), 100); // cancel after 100ms

      promise.catch(err => console.log(err.message)); // "Cancelled"

      1.1.0