try-catch-callback
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="2.0.1"></a> | ||
## [2.0.1](https://github.com/hybridables/try-catch-callback/compare/v2.0.0...v2.0.1) (2016-11-04) | ||
### Bug Fixes | ||
* **options:** introduce opts.return option ([57ec005](https://github.com/hybridables/try-catch-callback/commit/57ec005)) | ||
<a name="2.0.0"></a> | ||
@@ -7,0 +17,0 @@ # [2.0.0](https://github.com/hybridables/try-catch-callback/compare/v1.1.2...v2.0.0) (2016-10-31) |
30
index.js
@@ -36,3 +36,4 @@ /*! | ||
* @param {Array} `[opts.args]` custom argument(s) to be pass to `fn`, given value is arrayified | ||
* @param {Boolean} `[opts.passCallback]` pass `true` if you want `cb` to be passed to `fn` args. | ||
* @param {Boolean} `[opts.passCallback]` pass `true` if you want `cb` to be passed to `fn` args | ||
* @param {Boolean} `[opts.return]` if `true` returns error/value and does not calls `cb` | ||
* @param {Function} `[cb]` callback with `cb(err, res)` signature. | ||
@@ -53,26 +54,29 @@ * @return {Function} `thunk` if `cb` not given. | ||
} | ||
if (typeof cb !== 'function') { | ||
return function thunk (done) { | ||
tryCatch.call(this, fn, opts, done) | ||
} | ||
opts = opts && typeof opts === 'object' ? opts : {} | ||
if (opts.return || typeof cb === 'function') { | ||
return tryCatch.call(this, fn, opts, cb) | ||
} | ||
tryCatch.call(this, fn, opts, cb) | ||
return function thunk (done) { | ||
opts.thunk = true | ||
tryCatch.call(this, fn, opts, done) | ||
} | ||
} | ||
function tryCatch (fn, opts, cb) { | ||
if (typeof cb !== 'function') { | ||
if (opts.thunk && typeof cb !== 'function') { | ||
throw new TypeError('try-catch-callback: expect `cb` to be a function') | ||
} | ||
var options = opts && typeof opts === 'object' ? opts : {} | ||
var ctx = options.context || this | ||
var args = arrayify(options.args) | ||
var args = arrayify(opts.args) | ||
var ctx = opts.context || this | ||
var ret = null | ||
try { | ||
ret = fn.apply(ctx, options.passCallback ? args.concat(cb) : args) | ||
ret = fn.apply(ctx, opts.passCallback ? args.concat(cb) : args) | ||
} catch (err) { | ||
return cb(err) | ||
return opts.return ? err : cb(err) | ||
} | ||
if (!options.passCallback) cb(null, ret) | ||
if (opts.return) return ret | ||
if (!opts.passCallback) cb(null, ret) | ||
} | ||
@@ -79,0 +83,0 @@ |
{ | ||
"name": "try-catch-callback", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "try/catch block with a callback, used in [try-catch-core][]. Use it when you don't care about asyncness so much and don't want guarantees. If you care use [try-catch-core][].", | ||
@@ -5,0 +5,0 @@ "repository": "hybridables/try-catch-callback", |
@@ -25,3 +25,3 @@ <p align="center"> | ||
### [tryCatchCallback](index.js#L44) | ||
### [tryCatchCallback](index.js#L45) | ||
> 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][]. | ||
@@ -35,3 +35,4 @@ | ||
* `[opts.args]` **{Array}**: custom argument(s) to be pass to `fn`, given value is arrayified | ||
* `[opts.passCallback]` **{Boolean}**: pass `true` if you want `cb` to be passed to `fn` args. | ||
* `[opts.passCallback]` **{Boolean}**: pass `true` if you want `cb` to be passed to `fn` args | ||
* `[opts.return]` **{Boolean}**: if `true` returns error/value and does not calls `cb` | ||
* `[cb]` **{Function}**: callback with `cb(err, res)` signature. | ||
@@ -102,3 +103,3 @@ * `returns` **{Function}** `thunk`: if `cb` not given. | ||
- [gana](https://www.npmjs.com/package/gana): Small and powerful template engine with only sync and async compile. The… [more](https://github.com/tunnckocore/gana#readme) | [homepage](https://github.com/tunnckocore/gana#readme "Small and powerful template engine with only sync and async compile. The mid-level between [es6-template][] and [gana-compile][].") | ||
- [try-catch-core](https://www.npmjs.com/package/try-catch-core): Asynchronous and sync tryCatch in one place. The callback is securely wrapped… [more](https://github.com/tunnckocore/try-catch-core#readme) | [homepage](https://github.com/tunnckocore/try-catch-core#readme "Asynchronous and sync tryCatch in one place. The callback is securely wrapped with a [dezalgo][] and [once][].") | ||
- [try-catch-core](https://www.npmjs.com/package/try-catch-core): Low-level package to handle completion and errors of sync or asynchronous functions… [more](https://github.com/hybridables/try-catch-core#readme) | [homepage](https://github.com/hybridables/try-catch-core#readme "Low-level package to handle completion and errors of sync or asynchronous functions, using [once][] and [dezalgo][] libs. Useful for and used in higher-level libs such as [always-done][] to handle completion of anything.") | ||
- [try-require-please](https://www.npmjs.com/package/try-require-please): Try to require the given module, failing loudly with default message if… [more](https://github.com/tunnckocore/try-require-please#readme) | [homepage](https://github.com/tunnckocore/try-require-please#readme "Try to require the given module, failing loudly with default message if module does not exists.") | ||
@@ -119,3 +120,3 @@ | ||
[once]: https://github.com/isaacs/once | ||
[try-catch-core]: https://github.com/tunnckocore/try-catch-core | ||
[try-catch-core]: https://github.com/hybridables/try-catch-core | ||
@@ -167,1 +168,2 @@ [npmjs-url]: https://www.npmjs.com/package/try-catch-callback | ||
[always-done]: https://github.com/hybridables/always-done |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16566
78
164