Registry / storage / p-async-cache

p-async-cache

JSON →
library1.0.3jsnpmunverified

Promise-based async cache that deduplicates concurrent lookups by caching the promise itself rather than the resolved value. Version 1.0.3 is stable with no recent updates (last published 2017). It wraps lru-cache to provide a simple API for caching async operations, including stale value support and automatic background refresh. Key differentiators: lightweight, no dependencies beyond lru-cache, and supports sync/async load functions.

npm install p-async-cache
INSTALL
IMPORT
SIG · P-ASYNC-CACHE
P
p-async-cache
storagejavascriptv1.0.3
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.

default
import PAC from 'p-async-cache'
import { PAC } from 'p-async-cache'
Default export only; named import will not work.
PAC
const PAC = require('p-async-cache').default
const PAC = require('p-async-cache')
CommonJS require must access .default because package uses ES modules.
PAC
import PAC from 'p-async-cache'
import * as PAC from 'p-async-cache'
Namespace import gives an object with a default property; direct usage fails.

Creates a PAC instance with an async load function, gets the same key twice, and shows counter remains 1 on second call.

import PAC from 'p-async-cache'; let counter = 0; const cache = new PAC({ async load(userId) { counter++; // Simulate async fetch return { id: userId, name: `User${userId}` }; } }); async function main() { const result1 = await cache.get(123); console.log(result1.value); // { id: 123, name: 'User123' } console.log(counter); // 1 const result2 = await cache.get(123); console.log(result2.value); // Same as above console.log(counter); // 1 (load not called again) } main();
Debug
Known issues
gotchaThe default export must be used; named imports like `import { PAC }` result in `undefined` because the package exports only a default.
fix
Use `import PAC from 'p-async-cache'`.
affects: >=1.0.0
gotchaCommonJS `require('p-async-cache')` returns an object with `default` property; you must use `.default` to get the constructor.
fix
Use `const PAC = require('p-async-cache').default`.
affects: >=1.0.0
deprecatedPackage relies on `lru-cache` v4 which has breaking changes in v5+. If you have a newer lru-cache version in your project, it may cause compatibility issues.
fix
Pin `lru-cache` to version 4.x in your dependencies.
affects: >=1.0.0
gotchaThe `get` method resolves to an object `{value, stale}`. Forgetting to destructure `.value` can lead to printing the whole object.
fix
Use `await cache.get(key)` and access `.value`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: PAC is not a constructor
Using named import `{ PAC }` or CommonJS without `.default`
fix
Use `import PAC from 'p-async-cache'` or `const PAC = require('p-async-cache').default`
Cannot find module 'p-async-cache'
Package not installed or missing dependency
fix
Run `npm install p-async-cache`
TypeError: cache.get(...).then is not a function
Forget to await the promise returned by `get`
fix
Add `await` before `cache.get(params)`
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
lru-cacherequiredruntime dependency for cache storage
Agent activity
16 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
p-async-cache — npm install p-async-cache · libregistry