Skip to content

Commit

Permalink
fix(return): handle when done is present, but return promise
Browse files Browse the repository at this point in the history
fixes #18
  • Loading branch information
tunnckoCore committed Apr 2, 2017
1 parent 229bbf2 commit 631823a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
12 changes: 11 additions & 1 deletion dist/redolent-testing.js
Expand Up @@ -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);
}
});
Expand All @@ -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;
12 changes: 11 additions & 1 deletion dist/redolent.common.js
Expand Up @@ -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);
}
});
Expand All @@ -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;
12 changes: 11 additions & 1 deletion dist/redolent.es.js
Expand Up @@ -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);
}
});
Expand All @@ -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;
12 changes: 11 additions & 1 deletion index.js
Expand Up @@ -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)
}
})
Expand All @@ -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'
)
}
11 changes: 11 additions & 0 deletions test.js
Expand Up @@ -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')) {
Expand Down

0 comments on commit 631823a

Please sign in to comment.