Standard asCallback
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.
Common errors
-
TypeError: callback must be a function
cause The second argument passed to `asCallback` was not a function.fixEnsure the second argument to `asCallback` is a valid Node.js-style callback function with `(err, result)` signature. -
UnhandledPromiseRejectionWarning: Unhandled promise rejection.
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.fixAlways 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. -
Cannot find module 'standard-as-callback'
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).fixRun `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).
Warnings
- gotcha 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.
- gotcha 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.
- gotcha `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').
Install
-
npm install standard-as-callback -
yarn add standard-as-callback -
pnpm add standard-as-callback
Imports
- asCallback
import { asCallback } from 'standard-as-callback';import asCallback from 'standard-as-callback';
- asCallback
const { asCallback } = require('standard-as-callback');const asCallback = require('standard-as-callback'); - AsCallbackFunction
import type { AsCallbackFunction } from 'standard-as-callback';
Quickstart
import asCallback from 'standard-as-callback';
// Example 1: A simple resolving promise
const resolvingPromise = new Promise<string>((resolve) => {
setTimeout(() => {
resolve('Operation successful!');
}, 500);
});
asCallback(resolvingPromise, (err, res) => {
if (err) {
console.error('Callback error (resolvingPromise):', err);
return;
}
console.log('Callback result (resolvingPromise):', res); // null, 'Operation successful!'
});
// Example 2: A promise that rejects
const rejectingPromise = new Promise<string>((_resolve, reject) => {
setTimeout(() => {
reject(new Error('Something went wrong!'));
}, 1000);
});
asCallback(rejectingPromise, (err, res) => {
if (err) {
console.error('Callback error (rejectingPromise):', (err as Error).message); // 'Something went wrong!'
return;
}
console.log('Callback result (rejectingPromise):', res);
});