From c893c5660b841783985a174aa46c217620d30dc4 Mon Sep 17 00:00:00 2001 From: Danny Kirchmeier Date: Wed, 17 Oct 2018 00:21:55 -0500 Subject: [PATCH] Add support for promisifying prototype functions Fixes #67 --- index.js | 4 +++- test.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 767bc91..df56221 100644 --- a/index.js +++ b/index.js @@ -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); + }; } else { ret = Object.create(Object.getPrototypeOf(input)); } diff --git a/test.js b/test.js index 6419fff..8f89d0d 100644 --- a/test.js +++ b/test.js @@ -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'; @@ -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); +});