@ctheory/promisify
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

@ctheory/promisify npm (scoped) npm type definitions

A simple TS typed function for promisifying callback based functions.

Usage

async/await syntax

import promisify from '@ctheory/promisify';

const callbackFunction = (arg1, arg2, callback) => {
  if (arg1 && arg2) {
    callback(null, 'Success');
  } else {
    callback(new Error('An error happened'));
  }
};

const promisedFunction = promisify(callbackFunction);

try {
  const result = await promisedFunction('arg1', 'arg2');
  // Use result as usual
} catch (err) {
  // Callbacks are assumed to use error first convention
  // The error will be thrown if present
  console.log('callbackFunction failed with an error', err);
}

Promise syntax

const promisedFunction = promisify(callbackFunction);

promisedFunction
  .then(result => {
    // Use the result
  })
  .catch(err => {
    // Handle the thrown error
  });

Readme

Keywords

none

Package Sidebar

Install

npm i @ctheory/promisify

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

3.83 kB

Total Files

6

Last publish

Collaborators

  • jchancehud