Package Exports
- node-promisify
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-promisify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
node-promisify
Convert callback function to a new function which returns a promise.
Function name and other own enumberable properties are preserved, as well as the execution context.
Custom promise can be used instead of the native.
Usage
var promisify = require('node-promisify');
promisify(function async(a, b, c, cb) {
cb(null, d, e, f);
}, -1).then(function (results) {
// results is [d, e, f]
});
pfn = promisify(fn, promise)
pfn = promisify(fn, argc)
pfn = promisify(fn, opts)
fn: Function the async function
opts: Object
- promise: Function Promise constructor to be used instead of the native.
- argc: Number The number of values should be delivered to the then callback, 1 by default. If
argc
is not 1, the callback will receive an array containing the exact number of resolved values. If specified as -1, all values are delivered.
Example
var promisify = require('..');
var glob = promisify(require('glob'));
var path = require('path');
glob('*.js', { cwd: path.join(__dirname, 'files') })
.then(function (files) {
console.log('promisish style:', files);
});
glob('*.js', { cwd: path.join(__dirname, 'files') }, function (err, files) {
console.log('original style:', files);
});
console.log('other methods:', glob.sync('*.js', { cwd: path.join(__dirname, 'files') }));
output:
⌘ node example/glob.js
other methods: [ 'a.js', 'b.js', 'c.js' ]
original style: [ 'a.js', 'b.js', 'c.js' ]
promisish style: [ 'a.js', 'b.js', 'c.js' ]