Skip to content

make generic async function return promise when callback not present

Notifications You must be signed in to change notification settings

thenables/callback-and-promise

Repository files navigation

callback-and-promise

NPM version build status Test coverage David deps node version Gittip

make generic async function return promise when callback not present.

Please use thenify and thenify-all instead!

  • Supports both callback and promise
  • Preserves function names
  • Uses a native promise implementation if available and tries to fall back to bluebird
  • Converts multiple arguments from the callback into an Array

Installation

$ npm install callback-and-promise --save

Usage

Examples

  • Promisifies a single function:
var promisify = require('callback-and-promise');

var somethingAsync = promisify(function somethingAsync(a, b, c, callback) {
  callback(null, a, b, c);
});

// return a promise when callback not present
somethingAsync(a, b, c).then().catch();

// common async style
somethingAsync(a, b, c, callback);
  • Promisifies all the selected functions in an object:
var promisifyAll = require('callback-and-promise/all');

var fs = promisifyAll(require('fs'), {}, [
  'readFile',
  'writeFile',
]);

fs.readFile(__filename).then(function (buffer) {
  console.log(buffer.toString());
});

APIs

var fn = promisify([name], fn)

  • name - custom function name
  • fn - the source function

var obj = promisifyAll(source, [obj], [methods])

  • source - the source object for the async functions
  • obj - the destination to set all the promisified methods
  • methods - an array of method names of source

License

MIT

About

make generic async function return promise when callback not present

Resources

Stars

Watchers

Forks

Packages

No packages published