Registry / development / thunky

thunky

JSON →
library1.1.0jsnpmunverified

Delay the evaluation of a paramless async function and cache the result. Version 1.1.0 is stable with no active development. It provides a simple thunk pattern for lazy initialization, caching the result after the first successful invocation. Supports both callback and Promise (via `thunky/promise`) styles. Lightweight with zero dependencies. Differentiators: minimal API, error handling that prevents caching on failure, and compatibility with Node.js and browsers via bundlers.

npm install thunky
INSTALL
IMPORT
SIG · THUNKY
T
thunky
developmentjavascriptv1.1.0
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.

thunky
import thunky from 'thunky'
const thunky = require('thunky')
ESM import (default export) works in Node.js >=12 with ESM support. For CJS, use require, but ESM is preferred.
thunky (promise)
import thunkyp from 'thunky/promise'
const thunkyp = require('thunky/promise')
Promise variant for async/await. Must use separate subpath import.
thunky (require CJS)
const thunky = require('thunky')
const { thunky } = require('thunky')
thunky is a default export, not named. Destructuring will give undefined.

Demonstrates both callback-based thunky and promise-based thunkyp, showing lazy evaluation and caching.

import thunky from 'thunky'; import thunkyp from 'thunky/promise'; const lazy = thunky((cb) => { setTimeout(() => cb('result'), 100); }); lazy((val) => console.log('callback:', val)); const lazyP = thunkyp(async () => { await new Promise(r => setTimeout(r, 100)); return 'promise result'; }); console.log('await:', await lazyP());
Debug
Known issues
deprecatedThe callback-based thunky is deprecated in favor of the Promise version.
fix
Migrate to thunky/promise and async/await for new code.
affects: >=1.1.0
breakingthunky/promise subpath is only available in version 1.1.0+
fix
Update to thunky@1.1.0 or later.
affects: <1.1.0
gotchaIf the thunk callback is called with an Error, the result is NOT cached, causing the inner function to be called again on next invocation.
fix
Ensure your async function does not call the callback with an Error for caching to work.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: thunky(...) is not a function
Using destructuring to import thunky as a named export (e.g., import { thunky } from 'thunky')
fix
Use default import: import thunky from 'thunky'
Error: Cannot find module 'thunky/promise'
Installed thunky version <1.1.0 or missing file
fix
Run npm install thunky@1.1.0
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
52 hits · last 30 days
node
46
Resources
packagethunky
thunky — npm install thunky · libregistry