Skip to content

Commit 1df636f

Browse files
committed
Fix bug with optional parameter promise functions
Previously, we didn't remove the callback parameter from the arguments when calling a promise function, this confused functions with optional parameters, since the last parameter would be the callback function.
1 parent 4057d40 commit 1df636f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ exports.fromPromise = function (fn) {
2020
return Object.defineProperty(function () {
2121
const cb = arguments[arguments.length - 1]
2222
if (typeof cb !== 'function') return fn.apply(this, arguments)
23-
else fn.apply(this, arguments).then(r => cb(null, r), cb)
23+
else {
24+
delete arguments[arguments.length - 1]
25+
arguments.length--
26+
fn.apply(this, arguments).then(r => cb(null, r), cb)
27+
}
2428
}, 'name', { value: fn.name })
2529
}

test/from-promise.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ test('promise function works with promises', t => {
3737
.catch(t.end)
3838
})
3939

40+
test('promise function optional param works with callbacks', t => {
41+
t.plan(4)
42+
fn.call({a: 'a'}, 1, (err, arr) => {
43+
t.ifError(err, 'should not error')
44+
t.is(arr[0].a, 'a')
45+
t.is(arr[1], 1)
46+
t.is(arr[2], undefined)
47+
t.end()
48+
})
49+
})
50+
51+
test('promise function optional param works with promises', t => {
52+
t.plan(3)
53+
fn.call({a: 'a'}, 1)
54+
.then(arr => {
55+
t.is(arr[0].a, 'a')
56+
t.is(arr[1], 1)
57+
t.is(arr[2], undefined)
58+
t.end()
59+
})
60+
.catch(t.end)
61+
})
62+
4063
test('promise function error works with callbacks', t => {
4164
t.plan(2)
4265
errFn(err => {

0 commit comments

Comments
 (0)