Registry / devops / cp-fetch

cp-fetch

JSON →
library0.3.2jsnpmunverified

cp-fetch is a lightweight wrapper around the Fetch API that returns cancelable CPromise objects (from c-promise2) instead of native Promises, enabling automatic abort of network requests when the promise is cancelled, rejected, or resolved in a race/all pattern. Version 0.3.2 (stable) works in both browser and Node.js via cross-fetch ponyfill, supporting timeouts, cancellation, and concurrent request management (e.g., auto-abort other requests in CPromise.race/all on failure). It differentiates from axios and wretch by deeply integrating cancellation into the promise chain, not just the fetch signal. Released under MIT license, with low maintenance frequency (last update 2021).

npm install cp-fetch
INSTALL
IMPORT
SIG · CP-FETCH
C
cp-fetch
devopsjavascriptv0.3.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cpFetch
const cpFetch = require('cp-fetch');
import cpFetch from 'cp-fetch'; // works only if using ESM compatible bundler, but default import is broken in some setups
CommonJS require is recommended for Node.js; ESM import may fail without proper bundler configuration. Use import from 'cp-fetch/lib/native' for native fetch.
CPromise
const CPromise = require('c-promise2');
CPromise is a peer dependency; you must install c-promise2 separately. Not re-exported from cp-fetch.
cpFetch (native fetch)
import cpFetch from 'cp-fetch/lib/native';
const cpFetch = require('cp-fetch/lib/native'); // works but only if your environment has global fetch
Use this import path to avoid cross-fetch dependency and use the global fetch (e.g., modern browsers, or Node 18+ with --experimental-fetch).

Demonstrates cancelable fetch with timeout using cp-fetch and CPromise, aborting the request after 1 second.

const cpFetch = require('cp-fetch'); const CPromise = require('c-promise2'); const url = 'https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s'; const chain = cpFetch(url, { timeout: 10000 }) .then(response => response.json()) .then(data => console.log('Done:', data)) .catch(err => console.log('Error:', err)); // Cancel after 1 second setTimeout(() => chain.cancel(), 1000);
Debug
Known issues
deprecatedcp-fetch wraps global fetch but returns CPromise; using native Promise methods (like .then) still returns CPromise, but cancellation only works if you use await or CPromise.all/race.
fix
Always use CPromise concurrency methods (CPromise.all, CPromise.race) for auto-abort; avoid mixing with native Promise.all.
affects: >=0.1.0
gotchaDefault import uses cross-fetch internally, which may not be available in all environments; the native import path 'cp-fetch/lib/native' relies on global fetch.
fix
For Node.js <18 or older browsers, use default import (includes cross-fetch). For modern environments, use native import to avoid polyfill overhead.
affects: >=0.3.0
gotchaCPromise.cancel() does not reject the promise; it settles it with a special cancellation reason. Using .then() on the chain may still execute if not carefully handled.
fix
Use CPromise.finally or check cancellation with 'chain.promiseIsCancelled' property to handle cancellation cleanup.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: fetch is not defined
Using the native import path ('cp-fetch/lib/native') in an environment without global fetch (e.g., Node.js <18).
fix
Use default import (require('cp-fetch')) which includes cross-fetch polyfill, or install a fetch polyfill (e.g., node-fetch) and assign to globalThis.
Cannot find module 'c-promise2'
c-promise2 is a peer dependency and must be installed separately.
fix
Run: npm install c-promise2
TypeError: cpFetch is not a function
Attempting to import cp-fetch with ES module import syntax in a Node.js CommonJS environment without proper bundler.
fix
Use require('cp-fetch') or configure your bundler (e.g., webpack) to handle CommonJS modules.
Upgrade
Version history
0.3.2latest on npm
Audit
Dependencies
c-promise2requiredCore promise library providing cancelable CPromise used by cp-fetch
cross-fetchoptionalPolyfill for fetch API to support Node.js in the default export
Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
cp-fetch — npm install cp-fetch · libregistry