Promisify Node
promisify-node is a utility library designed to convert Node.js callback-style functions, methods, or entire modules into functions that return Promises. The current stable version is 0.5.0, though the project appears to be in an abandoned state with the last significant update in June 2020. Its primary differentiator is the ability to recursively wrap entire modules, automatically identifying asynchronous functions, and the option to wrap methods on objects either in-place or returning a new, non-mutated object. It uses an internal Promise implementation (or relies on a polyfill if not natively available) via `nodegit-promise` in older versions. Given its inactivity, users should be cautious about long-term support or compatibility with modern Node.js features like native ESM.
Common errors
-
TypeError: callback is not a function
cause Attempting to promisify a function that does not conform to the Node.js callback pattern (i.e., `(err, result) => {}` as the last argument), or passing a synchronous function.fixEnsure the function passed to `promisify` strictly follows the Node.js error-first callback signature. For synchronous functions, no promisification is needed, as they return values directly. -
TypeError: Cannot read properties of undefined (reading 'then')
cause This error occurs when you call a promisified function but try to use `.then()` on a result that is not a Promise. This can happen if `promisify-node` failed to correctly wrap the function (e.g., due to misidentification or an unexpected function signature) and it still returns `undefined` or a non-Promise value.fixVerify that the function you are trying to promisify is indeed an asynchronous, callback-style function. Debug the `promisify` call to ensure it returns a Promise. If wrapping an object, confirm the method was replaced as expected.
Warnings
- gotcha By default, when promisifying an object or module, `promisify-node` mutates the original object by replacing its callback-style methods with promise-returning ones. If you need to preserve the original object, you must explicitly pass `true` as the third argument to `promisify`.
- breaking The package has not seen active development since June 2020. While marked 'stable' at version 0.5.0, this implies potential compatibility issues with newer Node.js versions, updated standards, or the absence of security patches for discovered vulnerabilities. It might not be compatible with native ESM modules without additional tooling.
- gotcha `promisify-node` attempts to automatically identify asynchronous functions based on heuristics (e.g., if a function's name ends with 'Sync' it's usually skipped, or if it expects a callback). This heuristic approach can sometimes lead to misidentification, either wrapping a synchronous function or failing to wrap an asynchronous one.
Install
-
npm install promisify-node -
yarn add promisify-node -
pnpm add promisify-node
Imports
- promisify
import { promisify } from 'promisify-node';const promisify = require('promisify-node'); - fsModulePromisified
const fs = require('promisify-node')('fs'); - wrapFunction
const wrappedFunc = promisify(callbackBasedFunction);
Quickstart
const promisify = require('promisify-node');
const fs = require('fs');
// Wrap the entire 'fs' module. The module is cloned and then its
// asynchronous methods are identified and promisified.
const promisifiedFs = promisify(fs);
// Use the promisified readFile function
promisifiedFs.readFile('/etc/passwd', 'utf8')
.then(contents => {
console.log('File contents (first 100 chars):', contents.substring(0, 100));
})
.catch(err => {
console.error('Error reading file:', err.message);
});
// Example of wrapping a single function
function callbackStyleAsyncFunction(value, cb) {
setTimeout(() => {
if (value) {
cb(null, 'Success with: ' + value);
} else {
cb(new Error('No value provided'));
}
}, 50);
}
const promisifiedFunc = promisify(callbackStyleAsyncFunction);
promisifiedFunc('Hello Promisify')
.then(result => console.log('Promisified function result:', result))
.catch(error => console.log('Promisified function error:', error.message));