From 27a3fdb8fa1b7183fbb93da96acb5413a73f91e2 Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Thu, 22 Sep 2016 16:58:24 +0300 Subject: [PATCH] fix(option): add 3rd argument `passCallback` pass `true` as third argument if you want `cb` to be passed to `fn` --- README.md | 3 ++- index.js | 11 ++++++----- test.js | 12 ++++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a0eb848..18c493e 100644 --- a/README.md +++ b/README.md @@ -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** * `` **{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`. diff --git a/index.js b/index.js index 9bd914f..3f0c4a1 100644 --- a/index.js +++ b/index.js @@ -31,32 +31,33 @@ * * @param {Function} `` 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) } diff --git a/test.js b/test.js index 5ace9c3..fa4c280 100644 --- a/test.js +++ b/test.js @@ -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) +})