Registry / devops / promise-memoize

promise-memoize

JSON →
library1.2.1jsnpmunverified

Memoize promise-returning functions with cache expiration and prefetch. v1.2.1 is the latest stable release. Key differentiators: prefetch occurs before expiry to avoid stale data, errors can be cached separately with a shorter TTL, and it supports custom argument serialization. Uses a simple string or JSON-based cache key resolution. Similar to p-memoize but with built-in prefetch.

npm install promise-memoize
INSTALL
IMPORT
SIG · PROMISE-MEMOIZE
P
promise-memoize
devopsjavascriptv1.2.1
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.

promiseMemoize
const promiseMemoize = require('promise-memoize')
import promiseMemoize from 'promise-memoize'
Package provides a CommonJS default export; ESM import may not work without a wrapper or bundler.
default export
const memoize = require('promise-memoize')
const { memoize } = require('promise-memoize')
The module exports a single function, not an object. Destructuring will result in undefined.
TypeScript usage
import memoize = require('promise-memoize')
import memoize from 'promise-memoize'
For TypeScript with CommonJS, use import = require syntax. Default ESM import only works if esModuleInterop is enabled.

Demonstrates memoization of an async function with maxAge and maxErrorAge, including cache clearing.

const memoize = require('promise-memoize'); async function fetchUser(id) { // Simulate async operation return new Promise(resolve => setTimeout(() => resolve({ id, name: 'User' + id }), 1000)); } const cachedFetchUser = memoize(fetchUser, { maxAge: 60000, maxErrorAge: 1000 }); async function main() { // First call: fetches data const user1 = await cachedFetchUser(1); console.log(user1); // Second call within 1 minute: returns cached data const user1again = await cachedFetchUser(1); console.log(user1again); // Clear cache cachedFetchUser.clear(); } main().catch(console.error);
Debug
Known issues
gotchaArguments must be uniquely castable to strings (default serializer uses simple string conversion). Non-stringifiable arguments (e.g., objects without toString) can cause collisions or errors.
fix
Pass a custom resolve function or use 'json' serializer for complex arguments.
affects: >=0.0.0
gotchaErrors are not cached by default (maxErrorAge: 0). If your function frequently fails, you'll repeatedly invoke it. Consider setting a small maxErrorAge to throttle retries.
fix
Set maxErrorAge to a positive number, e.g., 1000 for 1 second.
affects: >=0.0.0
gotchaThe returned memoized function has a .clear() method, but it is not enumerable or not always present in TS typings. Relying on it without proper typing may cause TypeScript errors.
fix
Type the function as having a clear method, e.g., const memoizedFn: typeof fetchUser & { clear(): void } = memoize(...)
affects: >=0.0.0
gotchamaxAge value of Infinity (default) caches forever; no automatic expiry. This can lead to memory leaks if used with dynamic arguments over time.
fix
Always provide a finite maxAge, or manually call clear() periodically.
affects: >=0.0.0
gotchaPrefetch triggers at 0.7 * maxAge. If maxAge is very short, prefetch may happen immediately after first fetch, doubling load. Choose maxAge > ~100ms to avoid this.
fix
Set maxAge to at least a few hundred milliseconds to space out calls.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: memoize is not a function
Using destructured import from CommonJS module: const { memoize } = require('promise-memoize')
fix
Change to: const memoize = require('promise-memoize')
Cannot find module 'promise-memoize'
Package not installed or typo in module name.
fix
Run: npm install promise-memoize
Error: Promise resolver undefined is not a function
Passing a non-function argument to memoize, e.g., a promise instead of a function that returns a promise.
fix
Ensure the first argument is a function that returns a promise/thenable.
Type 'typeof memoize' has no call signatures
TypeScript users using default ESM import without esModuleInterop.
fix
Use import memoize = require('promise-memoize') or enable esModuleInterop.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
promise-memoize — npm install promise-memoize · libregistry