Skip to content

Commit 5dc7050

Browse files
committed
Improved denodeify
1 parent 6c6149a commit 5dc7050

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/node-extensions.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ Promise.denodeify = function (fn, argumentCount) {
2222
if (err) reject(err)
2323
else resolve(res)
2424
})
25-
fn.apply(self, args)
25+
var res = fn.apply(self, args)
26+
if (res && res.then) {
27+
resolve(res)
28+
}
2629
})
2730
}
2831
}

test/extensions-tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ describe('extensions', function () {
5555
done()
5656
})
5757
})
58+
it('resolves correctly when the wrapped function returns a promise anyway', function (done) {
59+
function wrap(val, key, callback) {
60+
return new Promise(function(resolve, reject) {
61+
resolve({val: val, key: key})
62+
})
63+
}
64+
var pwrap = Promise.denodeify(wrap)
65+
pwrap(sentinel, 'foo')
66+
.then(function (wrapper) {
67+
assert(wrapper.val === sentinel)
68+
assert(wrapper.key === 'foo')
69+
done()
70+
})
71+
})
5872
})
5973
describe('Promise.nodeify(fn)', function () {
6074
it('converts a promise returning function into a callback function', function (done) {

0 commit comments

Comments
 (0)