Standard asCallback

2.1.0 · maintenance · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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.

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

view raw JSON →