`real-cancellable-promise` is a robust and lightweight library offering a cancellable Promise implementation for JavaScript and TypeScript. Unlike many other approaches that merely prevent callbacks from executing, this library focuses on propagating cancellation signals to the underlying asynchronous operations, such as network requests made with `fetch`, `axios`, or `jQuery.ajax`, thereby releasing resources and truly aborting tasks. It boasts zero dependencies and a minimal footprint, under 1 kB minified and gzipped. The current stable version is 1.2.3, with ongoing active maintenance reflected in its consistent bug fix releases. Key differentiators include its explicit support for 'real' cancellation and its compatibility with popular ecosystems like React (solving issues like `setState` after unmount and handling variable query parameters) and `react-query`'s cancellation features out-of-the-box. It supports modern browsers (excluding Internet Explorer) and Node.js 14+ (with `AbortController` functionality requiring Node 15+).
npm install real-cancellable-promiseVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create a `CancellablePromise` using `AbortController` for an HTTP `fetch` request, initiate the request, and then cancel it, explicitly handling the `Cancellation` error. It also shows a successful fetch for comparison.
Upgrade to version 1.2.3 or higher to ensure `Cancellation` propagates correctly through promise chains.
Update to version 1.2.1 or newer to resolve the memory leak issue, especially for applications involving numerous or long-lived cancellable operations.
Ensure your build tooling (e.g., Webpack, Rollup) or Node.js environment is configured to correctly handle ES module resolution if you experience issues with imports. Node.js 14+ is supported, with `AbortController` usage in Node 15+.
Ensure your `cancel` callback function passed to the `CancellablePromise` constructor calls `reject(new Cancellation(...))` or `abortController.abort(new Cancellation(...))` to properly signal cancellation.
Upgrade to version 1.1.1 or higher to benefit from improved type compatibility, allowing `CancellablePromise` instances to be used where `Promise` instances are expected.
Ensure you are creating an instance of `CancellablePromise` (e.g., `new CancellablePromise(...)`) and that you have correctly imported `CancellablePromise` from the package. Double-check that you are not mistakenly assigning a standard Promise to your `cancellablePromise` variable.
Add `import { Cancellation } from 'real-cancellable-promise';` (for ESM) or `const { Cancellation } = require('real-cancellable-promise');` (for CJS) at the top of your file.Add a `.catch()` block to your promise chain to handle `Cancellation` errors. It's often good practice to specifically check `if (error instanceof Cancellation)` to handle cancellations differently from other errors.
If in a CommonJS context, use `const { CancellablePromise } = require('real-cancellable-promise');`. If using a bundler, ensure it's configured to correctly transpile or resolve ESM. Ensure your Node.js version is compatible with the module format you are using (Node.js 14+ is generally supported, but `import` syntax might need specific configuration in older Node.js environments).No dependency data recorded yet.