Skip to content

Commit

Permalink
fix(option): add 3rd argument passCallback
Browse files Browse the repository at this point in the history
pass `true` as third argument if you want `cb` to be passed to `fn`
  • Loading branch information
tunnckoCore committed Sep 22, 2016
1 parent 5f8111d commit 27a3fdb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ npm i try-catch-callback --save
const tryCatchCallback = require('try-catch-callback')
```

### [tryCatchCallback](index.js#L40)
### [tryCatchCallback](index.js#L41)
> Pass a synchronous `fn` that returns some result and handle completion or errors in `cb` if given, otherwise it returns thunk which accepts that `cb`. It's possible to not work in "async mode", if that's the case try to use [try-catch-core][] for your case, which guarantees that `cb` is called only once and always in next tick, using [dezalgo][] and [once][].
**Params**

* `<fn>` **{Function}**: function to be called.
* `[cb]` **{Function}**: callback with `cb(err, res)` signature.
* `[passCallback]` **{Function}**: pass the `cb` to `fn` when calling it.
* `returns` **{Function}** `thunk`: if `cb` not given.
* `throws` **{TypError}** if `fn` not a function.
* `throws` **{TypError}** if no function is passed to `thunk`.
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,33 @@
*
* @param {Function} `<fn>` function to be called.
* @param {Function} `[cb]` callback with `cb(err, res)` signature.
* @param {Function} `[passCallback]` pass the `cb` to `fn` when calling it.
* @return {Function} `thunk` if `cb` not given.
* @throws {TypError} if `fn` not a function.
* @throws {TypError} if no function is passed to `thunk`.
* @api public
*/

module.exports = function tryCatchCallback (fn, cb) {
module.exports = function tryCatchCallback (fn, cb, passCallback) {
if (typeof fn !== 'function') {
throw new TypeError('try-catch-callback: expect `fn` to be a function')
}
if (typeof cb !== 'function') {
return function thunk (done) {
tryCatch(fn, done)
tryCatch(fn, done, passCallback)
}
}
tryCatch(fn, cb)
tryCatch(fn, cb, passCallback)
}

function tryCatch (fn, cb) {
function tryCatch (fn, cb, passCallback) {
if (typeof cb !== 'function') {
throw new TypeError('try-catch-callback: expect `cb` to be a function')
}
var ret = null

try {
ret = fn()
ret = passCallback === true ? fn(cb) : fn()
} catch (err) {
if (!cb.called) return cb(err)
}
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ test('should return thunk if no `cb` passed', function (done) {
done()
})
})

test('should pass the `cb` to `fn` if 3rd arg is strictly `true`', function (done) {
tryCatch(function (cb) {
test.strictEqual(typeof cb, 'function')
}, done, true)
})

test('should `fn` not have arguments if 3rd arg is not `true`', function (done) {
tryCatch(function () {
test.strictEqual(arguments.length, 0)
}, done)
})

0 comments on commit 27a3fdb

Please sign in to comment.