Skip to content

Commit

Permalink
Add support for promisifying prototype functions (#69)
Browse files Browse the repository at this point in the history
Fixes #67
  • Loading branch information
danthegoodman authored and sindresorhus committed Oct 22, 2018
1 parent 696abf8 commit bbe89b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ module.exports = (input, options) => {

let ret;
if (objType === 'function') {
ret = (...args) => options.excludeMain ? input(...args) : processFn(input, options)(...args);
ret = function (...args) {
return options.excludeMain ? input(...args) : processFn(input, options).apply(this, args);
};

This comment has been minimized.

Copy link
@zhoupengqiang

zhoupengqiang Jan 15, 2019

processFn is an arrow function, does apply work?

} else {
ret = Object.create(Object.getPrototypeOf(input));
}
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ function FixtureClass() {
}
util.inherits(FixtureClass, FixtureParent);
FixtureClass.prototype.method1 = fixture;
FixtureClass.prototype.method2Cb = function (cb) {
setImmediate(() => cb(null, this.instanceValue1));
};
FixtureClass.prototype.method2Async = m(FixtureClass.prototype.method2Cb);
FixtureParent.prototype.overriddenValue1 = 4;
FixtureClass.prototype.value1 = 'neo';

Expand Down Expand Up @@ -269,3 +273,8 @@ test('class support - options.include over options.exclude', t => {
t.is(typeof pInstance.parentMethod1().then, 'function');
t.not(typeof pInstance.grandparentMethod1(() => {}).then, 'function');
});

test('promisify prototype function', async t => {
const instance = new FixtureClass();
t.is(await instance.method2Async(), 72);
});

0 comments on commit bbe89b7

Please sign in to comment.