Skip to content

Commit

Permalink
fill not passed args with undefined and pass cb as the last one
Browse files Browse the repository at this point in the history
  • Loading branch information
rmdm committed Mar 5, 2019
1 parent 30c35ec commit 16b45db
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "promback",
"version": "1.0.0",
"version": "1.0.1",
"description": "Level promises/callbacks landscape",
"main": "promback.js",
"scripts": {
Expand Down
17 changes: 12 additions & 5 deletions promback.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ function using (PromiseLib) {

return new PromiseLib(function (resolve, reject) {

if (fn.length !== args.length + 1) {
const argsDiff = fn.length - args.length

if (argsDiff <= 0) {
return resolve(fn.apply(that, args))
}

fn.call(that, ... args, function (err, value) {
if (err) { return reject(err) }
resolve(value)
})
fn.call(
that,
... args,
... new Array(argsDiff - 1),
function (err, value) {
if (err) { return reject(err) }
resolve(value)
}
)
})
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/promback.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ function testUsing (contextTitle, promiseLib) {

assert.strictEqual(result, undefined)
})

it('passes cb as the last arg, when the function expects more args than passed',
async function () {

function testFn (a, b, c, cb) {
cb(null, [ a, b, c ])
}

const fn = promback(testFn)

const result = await fn(1)

assert.deepStrictEqual(result, [ 1, undefined, undefined ])
})
})
})
}
Expand Down

0 comments on commit 16b45db

Please sign in to comment.