{"id":16224,"library":"standard-as-callback","title":"Standard asCallback","description":"standard-as-callback is a performant JavaScript library designed to register a Node.js-style error-first callback on a Promise. It provides functionality similar to the `.nodeify()` method popularized by the Bluebird Promise library, allowing for seamless integration of promise-based asynchronous operations into environments or APIs that expect callback conventions. The current stable version is 2.1.0, with its last update occurring in 2018, indicating a maintenance status rather than active development. Its key differentiator is providing a standardized, Bluebird-inspired approach to callback conversion, particularly useful in legacy Node.js projects or when bridging modern async/await patterns with older callback-expecting interfaces. The package ships with TypeScript type definitions.","status":"maintenance","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/luin/asCallback","tags":["javascript","ascallback","nodeify","promise","bluebird","typescript"],"install":[{"cmd":"npm install standard-as-callback","lang":"bash","label":"npm"},{"cmd":"yarn add standard-as-callback","lang":"bash","label":"yarn"},{"cmd":"pnpm add standard-as-callback","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily offers a default export for the asCallback function, common for single-function CommonJS modules.","wrong":"import { asCallback } from 'standard-as-callback';","symbol":"asCallback","correct":"import asCallback from 'standard-as-callback';"},{"note":"This is the standard CommonJS `require` pattern for modules with a default export, as shown in the original documentation.","wrong":"const { asCallback } = require('standard-as-callback');","symbol":"asCallback","correct":"const asCallback = require('standard-as-callback');"},{"note":"When using TypeScript, you can import the type definition for the asCallback function signature to ensure type safety, assuming the package exports this type directly.","symbol":"AsCallbackFunction","correct":"import type { AsCallbackFunction } from 'standard-as-callback';"}],"quickstart":{"code":"import asCallback from 'standard-as-callback';\n\n// Example 1: A simple resolving promise\nconst resolvingPromise = new Promise<string>((resolve) => {\n  setTimeout(() => {\n    resolve('Operation successful!');\n  }, 500);\n});\n\nasCallback(resolvingPromise, (err, res) => {\n  if (err) {\n    console.error('Callback error (resolvingPromise):', err);\n    return;\n  }\n  console.log('Callback result (resolvingPromise):', res); // null, 'Operation successful!'\n});\n\n// Example 2: A promise that rejects\nconst rejectingPromise = new Promise<string>((_resolve, reject) => {\n  setTimeout(() => {\n    reject(new Error('Something went wrong!'));\n  }, 1000);\n});\n\nasCallback(rejectingPromise, (err, res) => {\n  if (err) {\n    console.error('Callback error (rejectingPromise):', (err as Error).message); // 'Something went wrong!'\n    return;\n  }\n  console.log('Callback result (rejectingPromise):', res);\n});","lang":"typescript","description":"This code demonstrates how to use `standard-as-callback` to attach a Node.js-style error-first callback to both a successfully resolving promise and a rejecting promise. It showcases basic error handling within the callback and the expected output for each scenario."},"warnings":[{"fix":"Review the source code for potential vulnerabilities or compatibility issues with newer Node.js versions. Consider alternatives like direct Promise consumption or explicit conversion utilities if more active maintenance is desired.","message":"The package has not seen active development since 2018 (version 2.1.0). While stable, consider its long-term maintenance status when adopting for new projects or relying on modern JavaScript features.","severity":"gotcha","affected_versions":">=2.1.0"},{"fix":"Always ensure your callback function properly handles the `err` argument, even if just logging it, to prevent unhandled promise rejections. Example: `(err, res) => { if (err) { console.error(err); return; } /* handle success */ }`","message":"If the provided Promise rejects and no error-handling logic is included in the callback, the rejection might still become an unhandled promise rejection if not caught elsewhere in the Promise chain.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For converting callback-based APIs to Promises, use `util.promisify` (Node.js built-in), a dedicated promisification library, or manual Promise wrapping.","message":"`standard-as-callback` is designed to convert Promises into callback-style APIs. It is not intended for converting callback-based APIs into Promises (a process often called 'promisification').","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure the second argument to `asCallback` is a valid Node.js-style callback function with `(err, result)` signature.","cause":"The second argument passed to `asCallback` was not a function.","error":"TypeError: callback must be a function"},{"fix":"Always provide a callback that handles errors, e.g., `asCallback(promise, (err, res) => { if (err) { console.error(err); return; } /* success */ })`. Also ensure the promise chain itself handles rejections if `asCallback` is not the final error handler.","cause":"A Promise passed to `asCallback` rejected, but the provided callback did not handle the `err` argument, or no callback was provided, leading to the rejection not being consumed.","error":"UnhandledPromiseRejectionWarning: Unhandled promise rejection."},{"fix":"Run `npm install standard-as-callback` or `yarn add standard-as-callback`. Verify your import statement matches your module system (e.g., `require` for CommonJS, `import` for ESM).","cause":"The package is not installed, or there's a misconfiguration in your module resolution (e.g., incorrect CJS/ESM import in the wrong environment).","error":"Cannot find module 'standard-as-callback'"}],"ecosystem":"npm"}