Socket
Book a DemoInstallSign in
Socket

callback-and-promise

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

callback-and-promise

make generic async function return promise when callback not present

latest
npmnpm
Version
2.0.0
Version published
Weekly downloads
4
-71.43%
Maintainers
1
Weekly downloads
 
Created
Source

callback-and-promise

NPM version build status Test coverage David deps node version Gittip

make generic async function return promise when callback not present.

  • 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

Keywords

promise

FAQs

Package last updated on 25 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts