Skip to content

Commit

Permalink
do not pass callback when signatures does not match
Browse files Browse the repository at this point in the history
  • Loading branch information
rmdm committed Sep 26, 2018
1 parent ed128f6 commit 7e492c3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,27 @@ const resolveFive = promback(function () {
return Promise.resolve(5)
})

const resolveTen = promback(function () {
return 10
})

timeout(100)
.then(resolveFive)
.then(console.log) // logs 5
.then(resolveTen)
.then(console.log) // logs 10
```

API
===

### `promback (Function fn) -> async Function`

Wraps passed function and returns another one. The prombacked function passes its arguments to wrapped one and returns a promise fulfilled on one of the following cases:
Wraps passed function and returns prombacked one. The prombacked function passes its arguments to the wrapped one and returns a promise fulfilled on one of the following cases:

- wrapped function called callback added to the arguments,
- a promise returned by the wrapped function is fulfilled,
- wrapped function returned a value immediately,
- wrapped function thrown an error.

If the number of arguments of a prombacked function call does not match to the number of arguments expected by the wrapped function - 1, then callback is not passed to the wrapped function and its returned value resolved directly. This is done so to prevent passing an unexpected argument.

The passed callback signature is the normal node-style callback `(err, value)`.

Prombacked function's `this` reference left not bound to any particular object and is still dynamic.

### `promback.using (PromiseLib) -> promback`
Expand Down
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": "0.1.2",
"version": "0.1.3",
"description": "Level promises/callbacks landscape",
"main": "promback.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions promback.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function using (PromiseLib) {

return new PromiseLib(function (resolve, reject) {

if (fn.length !== args.length + 1) {
return resolve(fn.apply(that, args))
}

const result = fn.call(that, ... args, function (err, value) {
if (err) { reject(err) }
resolve(value)
Expand Down
12 changes: 12 additions & 0 deletions test/promback.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ function testUsing (contextTitle, promiseLib) {

assert.strictEqual(result, obj)
})

it('does not hang when cb not specified in function',
async function () {

const fn = promback(function () {

})

const result = await fn()

assert.strictEqual(result, undefined)
})
})
})
}
Expand Down
4 changes: 1 addition & 3 deletions test/readme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ describe('readme example', function () {
await timeout(100)
.then(resolveFive)
.then(console.log) // logs 5
.then(resolveTen)
.then(console.log) // logs 10

assert.deepStrictEqual(output, [ [ 5 ], [ 10 ] ])
assert.deepStrictEqual(output, [ [ 5 ] ])
})

it('uses bluebird promise', function () {
Expand Down

0 comments on commit 7e492c3

Please sign in to comment.