callback-and-promise
v2.0.0
Published
make generic async function return promise when callback not present
Downloads
11
Readme
callback-and-promise
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 namefn
- the source function
var obj = promisifyAll(source, [obj], [methods])
source
- the source object for the async functionsobj
- the destination to set all the promisified methodsmethods
- an array of method names of source
License
MIT