Skip to content

Commit 7178ebe

Browse files
Merge pull request then#24 from then/argument-count
Support passing `argumentCount` denodeify
2 parents ca5386e + 7b4b378 commit 7178ebe

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ Promise.from = function (value) {
5757

5858
return new ValuePromise(value)
5959
}
60-
Promise.denodeify = function (fn) {
60+
Promise.denodeify = function (fn, argumentCount) {
61+
argumentCount = argumentCount || Infinity
6162
return function () {
6263
var self = this
6364
var args = Array.prototype.slice.call(arguments)
6465
return new Promise(function (resolve, reject) {
66+
while (args.length && args.length > argumentCount) {
67+
args.pop()
68+
}
6569
args.push(function (err, res) {
6670
if (err) reject(err)
6771
else resolve(res)

test/extensions-tests.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('extensions', function () {
5757
})
5858
})
5959
})
60-
describe('Promise.denodeify(fn)', function () {
60+
describe('Promise.denodeify(fn, [argumentCount])', function () {
6161
it('returns a function that uses promises instead of callbacks', function (done) {
6262
function wrap(val, key, callback) {
6363
return callback(null, {val: val, key: key})
@@ -81,6 +81,18 @@ describe('extensions', function () {
8181
done()
8282
})
8383
})
84+
it('with an argumentCount it ignores extra arguments', function (done) {
85+
function wrap(val, key, callback) {
86+
return callback(null, {val: val, key: key})
87+
}
88+
var pwrap = Promise.denodeify(wrap, 2)
89+
pwrap(sentinel, 'foo', 'wtf')
90+
.then(function (wrapper) {
91+
assert(wrapper.val === sentinel)
92+
assert(wrapper.key === 'foo')
93+
done()
94+
})
95+
})
8496
})
8597
describe('Promise.nodeify(fn)', function () {
8698
it('converts a promise returning function into a callback function', function (done) {
@@ -301,4 +313,4 @@ describe('extensions', function () {
301313
})
302314
})
303315
})
304-
})
316+
})

0 commit comments

Comments
 (0)