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
$ npm install callback-and-promise --save
- 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());
});
name
- custom function namefn
- the source function
source
- the source object for the async functionsobj
- the destination to set all the promisified methodsmethods
- an array of method names of source
MIT