Skip to content

Commit

Permalink
refactor(arguments): switch arguments order, use latest dependencies
Browse files Browse the repository at this point in the history
update is-async-function and try-catch-callback to latest versions

BREAKING CHANGE: switch arguments order - options argument before done callback argument
  • Loading branch information
tunnckoCore committed Nov 1, 2016
1 parent f9f4f6f commit 0646c0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
18 changes: 11 additions & 7 deletions index.js
Expand Up @@ -62,25 +62,29 @@ var utils = require('./utils')
* ```
*
* @param {Function} `<fn>` function to call
* @param {Function} `[cb]` done callback to be used
* @param {Object} `[opts]` optional options passed to [try-catch-callback][]
* @return {Function} `thunk` only if `cb` is not a function
* @param {Function} `[cb]` done callback to be used
* @return {Function} `thunk` only if `cb` is not given
* @api public
*/

module.exports = function tryCatchCore (fn, cb, opts) {
module.exports = function tryCatchCore (fn, opts, cb) {
if (typeof fn !== 'function') {
throw new TypeError('try-catch-core: expect `fn` to be a function')
}
if (typeof opts === 'function') {
cb = opts
opts = null
}
if (typeof cb !== 'function') {
return function thunk (done) {
tryCatch.call(this, fn, done, cb || opts)
tryCatch.call(this, fn, opts, done)
}
}
tryCatch.call(this, fn, cb, opts)
tryCatch.call(this, fn, opts, cb)
}

function tryCatch (fn, cb, opts) {
function tryCatch (fn, opts, cb) {
if (typeof cb !== 'function') {
throw new TypeError('try-catch-core: expect `cb` to be a function')
}
Expand All @@ -94,5 +98,5 @@ function tryCatch (fn, cb, opts) {
? opts.passCallback
: true

utils.tryCatchCallback.call(this, fn, cb, opts)
utils.tryCatchCallback.call(this, fn, opts, cb)
}
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -22,10 +22,10 @@
},
"dependencies": {
"dezalgo": "^1.0.3",
"is-async-function": "^1.1.5",
"is-async-function": "^1.2.2",
"lazy-cache": "^2.0.1",
"once": "^1.4.0",
"try-catch-callback": "^1.1.2"
"try-catch-callback": "^2.0.0"
},
"devDependencies": {
"commitizen": "^2.8.6",
Expand Down
12 changes: 6 additions & 6 deletions test.js
Expand Up @@ -33,7 +33,7 @@ test('should throw TypeError if no function is passed to `thunk`', function (don
done()
})

test('should return thunk if `cb` (the 2nd arg) not a function', function (done) {
test('should return thunk if `cb` not a function', function (done) {
var thunk = tryCatchCore(function (next) {
next(null, 123)
})
Expand Down Expand Up @@ -100,25 +100,25 @@ test('should be able to pass custom arguments through options', function (done)
test.strictEqual(foo, 1)
test.strictEqual(bar, 2)
next(null, foo)
}, function (err, res) {
}, { args: [ 1, 2 ] }, function (err, res) {
test.strictEqual(err, null)
test.strictEqual(res, 1)
done()
}, { args: [ 1, 2 ] })
})
})

test('should not pass a callback to `fn` if passCallback:false', function (done) {
tryCatchCore(function () {
test.strictEqual(arguments.length, 0)
}, function (err, res) {
}, { passCallback: false }, function (err, res) {
test.strictEqual(err, null)
test.strictEqual(res, undefined)
done()
}, { passCallback: false })
})
})

test('should pass custom context to `fn` through options', function (done) {
tryCatchCore(function () {
test.strictEqual(this.foo, 'bar')
}, done, { context: { foo: 'bar' } })
}, { context: { foo: 'bar' } }, done)
})

0 comments on commit 0646c0a

Please sign in to comment.