Promisify Node

0.5.0 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to promisify an entire Node.js module (like 'fs') and a standalone callback-style function, then use their promise-returning interfaces.

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));

view raw JSON →