{"id":11606,"library":"putil-promisify","title":"putil-promisify","description":"putil-promisify is a lightweight utility designed for transforming traditional Node.js callback-style functions into Promise-based equivalents. Its current stable version is 1.10.1, indicating a mature and likely infrequently updated library, suggesting a maintenance release cadence. This package provides a straightforward API, primarily through `fromCallback`, to convert functions adhering to the `(err, data)` callback signature into Promise-returning functions. It serves as an alternative to Node.js's built-in `util.promisify` for scenarios requiring a minimal dependency or compatibility with Node.js versions older than 8.0.0 (though its `package.json` specifies Node.js >= 14.0), or for specific custom promisification needs where `util.promisify`'s behavior might not be ideal.","status":"maintenance","version":"1.10.1","language":"javascript","source_language":"en","source_url":"https://github.com/panates/putil-promisify","tags":["javascript","async","promise","promisify","typescript"],"install":[{"cmd":"npm install putil-promisify","lang":"bash","label":"npm"},{"cmd":"yarn add putil-promisify","lang":"bash","label":"yarn"},{"cmd":"pnpm add putil-promisify","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For modern JavaScript environments, `fromCallback` is typically imported as a named export. Ensure your project is configured for ESM if using `import` statements.","wrong":"import Promisify from 'putil-promisify'; // Incorrect default import, 'fromCallback' is a named export.","symbol":"fromCallback (ESM)","correct":"import { fromCallback } from 'putil-promisify';"},{"note":"In CommonJS environments, the package exports an object. The main utility function `fromCallback` is a property of this exported object.","wrong":"const { fromCallback } = require('putil-promisify'); // The primary export is an object, not a direct destructure of 'fromCallback'.","symbol":"fromCallback (CommonJS)","correct":"const Promisify = require('putil-promisify');\nconst promisifiedFunction = Promisify.fromCallback(callbackStyleFunction);"},{"note":"To leverage TypeScript's type checking, import `Promisify` as a type for your promisified functions or related constructs. The library ships with its own type definitions.","symbol":"TypeScript types","correct":"import type { Promisify } from 'putil-promisify';"}],"quickstart":{"code":"import { fromCallback } from 'putil-promisify';\nimport * as fs from 'fs';\n\n// A simple example of a callback-style function\nfunction asyncOperation(value: string, callback: (err: Error | null, result?: string) => void) {\n  setTimeout(() => {\n    if (value === 'error') {\n      return callback(new Error('Simulated error for: ' + value));\n    }\n    callback(null, 'Processed: ' + value.toUpperCase());\n  }, 100);\n}\n\n// Promisify the callback-style function\nconst promisifiedAsyncOperation = fromCallback(asyncOperation);\n\n// Use the promisified function with async/await\nasync function runExample() {\n  try {\n    const result1 = await promisifiedAsyncOperation('hello');\n    console.log(result1);\n\n    // Promisify a Node.js built-in function like fs.readFile\n    const readFilePromise = fromCallback(fs.readFile);\n    const fileContent = await readFilePromise('./package.json', 'utf8');\n    console.log('\\nContent of package.json (first 100 chars):\\n', fileContent.substring(0, 100) + '...');\n\n    const result2 = await promisifiedAsyncOperation('world');\n    console.log(result2);\n\n    // Demonstrate error handling\n    await promisifiedAsyncOperation('error');\n  } catch (error: any) {\n    console.error('\\nCaught an error:', error.message);\n  }\n}\n\nrunExample();","lang":"typescript","description":"This quickstart demonstrates how to use `fromCallback` to convert both a custom callback-style function and a Node.js built-in function (like `fs.readFile`) into Promise-based equivalents, then utilizes them with `async/await` for cleaner asynchronous code, including error handling."},"warnings":[{"fix":"For Node.js v8.0.0 and above, use `import { promisify } from 'node:util';` or `const { promisify } = require('node:util');`. Consider `putil-promisify` only if targeting older Node.js versions or if its specific implementation is required.","message":"Node.js has included a native `util.promisify` function since v8.0.0, which is the recommended standard for converting Node.js-style callback functions to Promises. For most modern Node.js applications (v8+), `util.promisify` from `node:util` is generally preferred as it is part of the runtime and often handles nuances more robustly.","severity":"deprecated","affected_versions":"<=1.x"},{"fix":"Always defer to the `engines` field in `package.json` and ensure your Node.js environment is v14.0 or newer to guarantee compatibility and stability. Using older versions might lead to unexpected issues.","message":"There is a discrepancy in the stated Node.js compatibility: the README claims `node >= 6.x`, but the `package.json` specifies `engines: {\"node\": \">= 14.0\"}`. This suggests that while older Node.js versions *might* have been supported historically, the current official support is for Node.js v14.0 and newer.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Thoroughly test promisified functions, especially those with complex or non-standard callback patterns. For such cases, a custom wrapper function might be more appropriate, or `util.promisify.custom` if using the native Node.js utility.","message":"Like many promisification utilities, `putil-promisify` (and even `util.promisify`) is designed for functions that follow the Node.js error-first callback pattern `(err, result) => ...`. Functions that use non-standard callback signatures (e.g., `(data1, data2, err) => ...` or multiple success arguments `(err, result1, result2) => ...`) may not be promisified correctly, potentially leading to unexpected Promise resolutions or rejections.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For CommonJS, ensure you import the whole module first: `const Promisify = require('putil-promisify');`, then access `Promisify.fromCallback`. For ESM, use `import { fromCallback } from 'putil-promisify';` to directly import the named export.","cause":"This error typically occurs when `putil-promisify` is imported incorrectly, either by attempting a direct named destructuring in CommonJS or an incorrect default import in ESM, preventing `fromCallback` from being accessed as a method.","error":"TypeError: Promisify.fromCallback is not a function"},{"fix":"Verify that the function you are attempting to promisify accepts a `(error, result)` callback as its final argument. If it does not, you may need to wrap it in an adapter function that conforms to this signature before passing it to `fromCallback`.","cause":"This error indicates that the function passed to `fromCallback` does not accept a callback as its last argument, or the argument provided is not callable. `putil-promisify` expects the input function to strictly adhere to the Node.js error-first callback signature.","error":"Error: The 'callback' argument must be a function"}],"ecosystem":"npm"}