diff --git a/dist/redolent-testing.js b/dist/redolent-testing.js index 127b144..a008716 100644 --- a/dist/redolent-testing.js +++ b/dist/redolent-testing.js @@ -118,8 +118,9 @@ function redolent (fn, opts) { opts.args = isAsyncFn ? opts.args.concat(done) : opts.args; var syncResult = fn.apply(opts.context, opts.args); + var xPromise = isAsyncFn && !called && isPromise(syncResult, opts.Promise); - if (!isAsyncFn && !called) { + if ((!isAsyncFn && !called) || xPromise) { resolve(syncResult); } }); @@ -134,4 +135,13 @@ function normalize (promise, Ctor) { return promise } +function isPromise (val, Promize$$1) { + return val instanceof Promize$$1 || ( + val !== null && + typeof val === 'object' && + typeof val.then === 'function' && + typeof val.catch === 'function' + ) +} + module.exports = redolent; diff --git a/dist/redolent.common.js b/dist/redolent.common.js index 01e3996..b77f735 100644 --- a/dist/redolent.common.js +++ b/dist/redolent.common.js @@ -392,8 +392,9 @@ function redolent (fn, opts) { opts.args = isAsyncFn ? opts.args.concat(done) : opts.args; var syncResult = fn.apply(opts.context, opts.args); + var xPromise = isAsyncFn && !called && isPromise(syncResult, opts.Promise); - if (!isAsyncFn && !called) { + if ((!isAsyncFn && !called) || xPromise) { resolve(syncResult); } }); @@ -408,4 +409,13 @@ function normalize (promise, Ctor) { return promise } +function isPromise (val, Promize$$1) { + return val instanceof Promize$$1 || ( + val !== null && + typeof val === 'object' && + typeof val.then === 'function' && + typeof val.catch === 'function' + ) +} + module.exports = redolent; diff --git a/dist/redolent.es.js b/dist/redolent.es.js index 61c8318..341b487 100644 --- a/dist/redolent.es.js +++ b/dist/redolent.es.js @@ -388,8 +388,9 @@ function redolent (fn, opts) { opts.args = isAsyncFn ? opts.args.concat(done) : opts.args; var syncResult = fn.apply(opts.context, opts.args); + var xPromise = isAsyncFn && !called && isPromise(syncResult, opts.Promise); - if (!isAsyncFn && !called) { + if ((!isAsyncFn && !called) || xPromise) { resolve(syncResult); } }); @@ -404,4 +405,13 @@ function normalize (promise, Ctor) { return promise } +function isPromise (val, Promize$$1) { + return val instanceof Promize$$1 || ( + val !== null && + typeof val === 'object' && + typeof val.then === 'function' && + typeof val.catch === 'function' + ) +} + export default redolent; diff --git a/index.js b/index.js index 954a4f6..ebf864f 100644 --- a/index.js +++ b/index.js @@ -118,8 +118,9 @@ export default function redolent (fn, opts) { opts.args = isAsyncFn ? opts.args.concat(done) : opts.args var syncResult = fn.apply(opts.context, opts.args) + var xPromise = isAsyncFn && !called && isPromise(syncResult, opts.Promise) - if (!isAsyncFn && !called) { + if ((!isAsyncFn && !called) || xPromise) { resolve(syncResult) } }) @@ -133,3 +134,12 @@ function normalize (promise, Ctor) { promise.___customPromise = Boolean(Ctor.___customPromise) return promise } + +function isPromise (val, Promize) { + return val instanceof Promize || ( + val !== null && + typeof val === 'object' && + typeof val.then === 'function' && + typeof val.catch === 'function' + ) +} diff --git a/test.js b/test.js index c8823e8..71ca906 100644 --- a/test.js +++ b/test.js @@ -158,6 +158,17 @@ function factory (promisify) { }) .catch(done) }) + + test('should work if `done` is present, but return a promise', function (done) { + var fn = promisify(function (xxx, done) { + return Pinkie.resolve(100 + xxx) + }) + + return fn(200).then(function (val) { + test.strictEqual(val, 300) + done() + }, done).catch(done) + }) } if (semver.lt(process.version, '0.12.0')) {