{"id":45843,"library":"promise-memoize","title":"promise-memoize","description":"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.","status":"active","version":"1.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/nodeca/promise-memoize","tags":["javascript","promise","memoize","cache"],"install":[{"cmd":"npm install promise-memoize","lang":"bash","label":"npm"},{"cmd":"yarn add promise-memoize","lang":"bash","label":"yarn"},{"cmd":"pnpm add promise-memoize","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package provides a CommonJS default export; ESM import may not work without a wrapper or bundler.","wrong":"import promiseMemoize from 'promise-memoize'","symbol":"promiseMemoize","correct":"const promiseMemoize = require('promise-memoize')"},{"note":"The module exports a single function, not an object. Destructuring will result in undefined.","wrong":"const { memoize } = require('promise-memoize')","symbol":"default export","correct":"const memoize = require('promise-memoize')"},{"note":"For TypeScript with CommonJS, use import = require syntax. Default ESM import only works if esModuleInterop is enabled.","wrong":"import memoize from 'promise-memoize'","symbol":"TypeScript usage","correct":"import memoize = require('promise-memoize')"}],"quickstart":{"code":"const memoize = require('promise-memoize');\n\nasync function fetchUser(id) {\n  // Simulate async operation\n  return new Promise(resolve => setTimeout(() => resolve({ id, name: 'User' + id }), 1000));\n}\n\nconst cachedFetchUser = memoize(fetchUser, { maxAge: 60000, maxErrorAge: 1000 });\n\nasync function main() {\n  // First call: fetches data\n  const user1 = await cachedFetchUser(1);\n  console.log(user1);\n  // Second call within 1 minute: returns cached data\n  const user1again = await cachedFetchUser(1);\n  console.log(user1again);\n  // Clear cache\n  cachedFetchUser.clear();\n}\n\nmain().catch(console.error);","lang":"javascript","description":"Demonstrates memoization of an async function with maxAge and maxErrorAge, including cache clearing."},"warnings":[{"fix":"Pass a custom resolve function or use 'json' serializer for complex arguments.","message":"Arguments 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.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Set maxErrorAge to a positive number, e.g., 1000 for 1 second.","message":"Errors 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.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Type the function as having a clear method, e.g., const memoizedFn: typeof fetchUser & { clear(): void } = memoize(...)","message":"The 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.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Always provide a finite maxAge, or manually call clear() periodically.","message":"maxAge value of Infinity (default) caches forever; no automatic expiry. This can lead to memory leaks if used with dynamic arguments over time.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Set maxAge to at least a few hundred milliseconds to space out calls.","message":"Prefetch 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.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"search_vec":"'argument':43 'avoid':27 'base':52 'built':64 'built-in':63 'cach':10,33,53,70 'custom':42 'data':29 'differenti':21 'error':30 'expir':11 'expiri':25 'function':8 'javascript':67 'json':51 'json-bas':50 'key':20,54 'latest':17 'memoiz':3,4,60,69 'occur':23 'p':59 'p-memoiz':58 'prefetch':13,22,66 'promis':2,6,68 'promise-memo':1 'promise-return':5 'releas':19 'resolut':55 'return':7 'separ':34 'serial':44 'shorter':37 'similar':56 'simpl':47 'stabl':18 'stale':28 'string':48 'support':41 'ttl':38 'use':45 'v1.2.1':14","created_at":"2026-06-07T12:57:00.014649+00:00","updated_at":"2026-06-07T12:57:00.014649+00:00","problems":[{"fix":"Change to: const memoize = require('promise-memoize')","cause":"Using destructured import from CommonJS module: const { memoize } = require('promise-memoize')","error":"TypeError: memoize is not a function"},{"fix":"Run: npm install promise-memoize","cause":"Package not installed or typo in module name.","error":"Cannot find module 'promise-memoize'"},{"fix":"Ensure the first argument is a function that returns a promise/thenable.","cause":"Passing a non-function argument to memoize, e.g., a promise instead of a function that returns a promise.","error":"Error: Promise resolver undefined is not a function"},{"fix":"Use import memoize = require('promise-memoize') or enable esModuleInterop.","cause":"TypeScript users using default ESM import without esModuleInterop.","error":"Type 'typeof memoize' has no call signatures"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/nodeca/promise-memoize#readme","github":"https://github.com/nodeca/promise-memoize","docs":null,"changelog":null,"pypi":null,"npm":"promise-memoize","openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}