Promisify methods on the prototype of an object
npm install pify-protoinclude/exclude/multiArgs options (I don't need the others; always open to PRs)
pify,
$ npm install --save pify-proto
`
Usage
`js
const pify = require('pify-proto');
class SomeConstructor {
constructor () {
this.foo = 'bar'
}
baz (cb) {
cb(null, this.foo)
}
}
// promisify prototype of an object
const inst = pify(new SomeConstructor())
inst.baz().then(console.log.bind(console))
//=> 'foo'
`
API
$3
Returns a new object with prototype methods on obj promisified.
#### input
Type: object
The object to promisify.
#### promiseModule
Type: function
Custom promise module to use instead of the native one.
Check out pinkie-promise if you need a tiny promise polyfill.
#### options
##### include
Type: array of (string|regex)
Methods in a module to promisify. Remaining methods will be left untouched.
##### exclude
Type: array of (string|regex)
Default: [/.+Sync$/]
Methods in a module not to promisify. Methods with names ending with 'Sync' are excluded by default.
##### multiArgs
Type: boolean
Default: false
By default, the promisified methods will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like oauth with prototype methods that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument.
`js
const OAuth = require('oauth').OAuth;
const pify = require('pify-proto');
const oauth = pify(new OAuth(
'http://blah',
'http://blah',
'blah blah'
), {multiArgs: true});
oauth.getOAuthRequestToken()
.then(result => {
const [ oauth_token, oauth_secret ] = result;
});
`
Authors & License
pify was created by Sindre Sorhus.
pify-proto was created by Sam Gluck.
pify-proto contributors:
* Scott Donnelly (@sdd)
pify-proto` is released under the MIT license.