Registry / http-networking / wrappy

wrappy

JSON →
library0.2.7jsnpmunverified

Wrappy is a tiny, foundational utility for reliably wrapping callback functions in JavaScript. Its primary function is to ensure that a given callback is invoked at most once, which is crucial for preventing unexpected behavior in asynchronous operations where a callback might inadvertently be called multiple times. Currently stable at version 1.0.2, released in 2015, the package maintains an extremely low release cadence due to its minimal scope and high stability, with updates typically addressing only critical edge cases or compatibility. Wrappy is largely an internal dependency for many other npm packages (e.g., `once`, `inflight`) rather than a direct application-level utility, providing a robust and dependency-free solution for basic callback management.

npm install wrappy
INSTALL
IMPORT
SIG · WRAPPY
W
wrappy
http-networkingjavascriptv0.2.7
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

wrappy
const wrappy = require('wrappy')
CommonJS module import, standard for Node.js environments.
wrappy
import wrappy from 'wrappy'
import { wrappy } from 'wrappy'
When using in an ES Module (ESM) environment or with bundlers, `wrappy` is treated as a default export from its CommonJS nature. Attempting a named import (`import { wrappy } from 'wrappy'`) will fail as no such named export exists.
Callback Wrapper
const onceWrapper = wrappy(callbackFn)
const onceWrapper = wrappy.wrappy(callbackFn)
The `wrappy` function itself is the module's export, not a property of an object. Call it directly after importing.

This quickstart demonstrates how to use `wrappy` to ensure a callback function is executed at most once, even if called multiple times. It includes a basic asynchronous simulation and an example showing how `wrappy` can manage multiple asynchronous completion signals into a single callback trigger.

const wrappy = require('wrappy'); // Define a callback function that we want to ensure runs only once let executionCount = 0; function myCallback(err, data) { if (err) { console.error('Error:', err); return; } executionCount++; console.log(`Callback executed! Data: ${data}. Total executions: ${executionCount}`); } // Wrap the callback using wrappy const wrappedCallback = wrappy(myCallback); // Simulate an asynchronous operation where the callback might be called multiple times console.log('--- Starting async operation simulation ---'); setTimeout(() => { console.log('Attempting to call wrappedCallback (first time)'); wrappedCallback(null, 'Initial Data'); }, 100); setTimeout(() => { console.log('Attempting to call wrappedCallback (second time - should be ignored)'); wrappedCallback(null, 'More Data'); // This call should be ignored by wrappy }, 200); setTimeout(() => { console.log('Attempting to call wrappedCallback (third time - should be ignored)'); wrappedCallback(null, 'Even More Data'); // This call should also be ignored }, 300); // Example of wrappy being used for error handling that only triggers once function saveUserData(user, callback) { const done = wrappy(callback); let dbOperationComplete = false; let fileWriteComplete = false; // Simulate two async operations that need to complete, but only one `done` call setTimeout(() => { if (Math.random() > 0.9) return done(new Error('DB connection failed')); dbOperationComplete = true; console.log('DB operation finished.'); if (fileWriteComplete) done(null, 'User data saved successfully'); }, 150); setTimeout(() => { if (Math.random() > 0.9) return done(new Error('File write failed')); fileWriteComplete = true; console.log('File write finished.'); if (dbOperationComplete) done(null, 'User data saved successfully'); }, 250); } saveUserData({ id: 1, name: 'Alice' }, (err, result) => { if (err) { console.error('Save user data failed:', err.message); return; } console.log('Save user data success:', result); });
Debug
Known issues
gotchaWrappy is a low-level utility primarily designed for internal use by library authors, especially for managing asynchronous control flow in older CommonJS patterns. For direct application development, modern alternatives like Promises, async/await, or dedicated event emitters often provide more idiomatic and robust solutions for managing asynchronous operations and ensuring single execution.
fix
Consider if Promises, async/await, or other modern async patterns are more suitable for your application's needs before directly integrating `wrappy`.
affects: >=1.0.0
gotchaThe `wrappy` package is a CommonJS module. When used in an ES Module (ESM) environment (e.g., with `type: "module"` in `package.json` or in a browser build process like Webpack/Rollup), it must be imported as a default export (`import wrappy from 'wrappy'`). Attempting a named import (`import { wrappy } from 'wrappy'`) will result in runtime errors as the named export `wrappy` does not exist.
fix
Always use `import wrappy from 'wrappy'` for ESM environments. Avoid named imports.
affects: >=1.0.0
gotchaWrappy explicitly guarantees a function is called *at most once*. It does not provide features like retry mechanisms, advanced error handling (beyond passing the error), debouncing, throttling, or complex flow control (e.g., parallel execution, sequencing tasks). Developers needing these more elaborate features should explore other specialized libraries or implement them using Promises.
fix
For more advanced asynchronous control, consider libraries like `async`, `lodash.debounce`, `lodash.throttle`, or implement custom logic using Promises.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0 , wrappy__WEBPACK_IMPORTED_MODULE_0__.wrappy) is not a function
This error typically occurs in bundled ES Module (ESM) contexts (e.g., Webpack, Rollup) when `wrappy` is incorrectly imported using a named import syntax (`import { wrappy } from 'wrappy'`) instead of a default import.
fix
Change the import statement from `import { wrappy } from 'wrappy'` to `import wrappy from 'wrappy'`.
TypeError: wrappy is not a function
This can occur if `wrappy` is imported incorrectly in a CommonJS module, or if the imported `wrappy` variable is not the function itself but perhaps an object, or if it's being called as a method on an undefined object. It often signifies a mismatch in how the module's export is consumed.
fix
Ensure you are using `const wrappy = require('wrappy');` in CommonJS and calling `wrappy(callback)` directly. Verify that the `wrappy` variable holds the function reference before attempting to call it.
Upgrade
Version history
0.2.7latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
45 hits · last 30 days
node
38
OpenAI (training)
1
Resources
wrappy — npm install wrappy · libregistry