Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

try-catch-callback

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

try-catch-callback - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

15

CHANGELOG.md

@@ -5,2 +5,17 @@ # Change Log

<a name="1.1.0"></a>
# [1.1.0](https://github.com/tunnckocore/try-catch-callback/compare/v1.0.2...v1.1.0) (2016-10-30)
### Bug Fixes
* **opts:** fix passing options when want thunk ([f80c7d5](https://github.com/tunnckocore/try-catch-callback/commit/f80c7d5))
### Features
* **options:** add options object ([3e35605](https://github.com/tunnckocore/try-catch-callback/commit/3e35605)), closes [#1](https://github.com/tunnckocore/try-catch-callback/issues/1)
<a name="1.0.2"></a>

@@ -7,0 +22,0 @@ ## [1.0.2](https://github.com/tunnckocore/try-catch-callback/compare/v1.0.1...v1.0.2) (2016-09-22)

28

index.js

@@ -34,3 +34,6 @@ /*!

* @param {Function} `[cb]` callback with `cb(err, res)` signature.
* @param {Boolean} `[passCallback]` pass `true` if you want `cb` to be passed to `fn` args.
* @param {Object} `[opts]` optional options, such as `context` and `args`
* @param {Object} `[opts.context]` context to be passed to `fn`
* @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.
* @return {Function} `thunk` if `cb` not given.

@@ -42,3 +45,3 @@ * @throws {TypError} if `fn` not a function.

module.exports = function tryCatchCallback (fn, cb, passCallback) {
module.exports = function tryCatchCallback (fn, cb, opts) {
if (typeof fn !== 'function') {

@@ -49,21 +52,30 @@ throw new TypeError('try-catch-callback: expect `fn` to be a function')

return function thunk (done) {
tryCatch(fn, done, passCallback)
tryCatch.call(this, fn, done, cb || opts)
}
}
tryCatch(fn, cb, passCallback)
tryCatch.call(this, fn, cb, opts)
}
function tryCatch (fn, cb, passCallback) {
function tryCatch (fn, cb, opts) {
if (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 ret = null
try {
ret = passCallback === true ? fn(cb) : fn()
ret = fn.apply(ctx, options.passCallback ? args.concat(cb) : args)
} catch (err) {
if (!cb.called) return cb(err)
return cb(err)
}
if (!cb.called) return cb(null, ret)
cb(null, ret)
}
function arrayify (val) {
if (!val) return []
if (Array.isArray(val)) return val
return [val]
}
{
"name": "try-catch-callback",
"version": "1.0.2",
"version": "1.1.0",
"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][].",

@@ -16,3 +16,3 @@ "repository": "tunnckoCore/try-catch-callback",

"coverage": "nyc node test.js",
"lint:coverage": "nyc check-coverage --lines 100 --statements 100 --functions 100",
"lint:coverage": "nyc check-coverage --lines 100 --branches 100 --statements 100 --functions 100",
"report-coverage": "nyc report --reporter=text-lcov | coveralls",

@@ -73,3 +73,6 @@ "prerelease": "npm test",

"once",
"try-catch-core"
"try-catch-core",
"es6-template",
"gana",
"gana-compile"
],

@@ -76,0 +79,0 @@ "lint": {

@@ -19,3 +19,3 @@ # [try-catch-callback][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url] [![npm downloads][downloads-img]][downloads-url]

### [tryCatchCallback](index.js#L41)
### [tryCatchCallback](index.js#L44)
> 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][].

@@ -27,6 +27,7 @@

* `[cb]` **{Function}**: callback with `cb(err, res)` signature.
* `[passCallback]` **{Boolean}**: pass `true` if you want `cb` to be passed to `fn` args.
* `[opts]` **{Object}**: optional options, such as `context` and `args`
* `[opts.context]` **{Object}**: context to be passed to `fn`
* `[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.
* `returns` **{Function}** `thunk`: if `cb` not given.
* `throws` **{TypError}** if `fn` not a function.
* `throws` **{TypError}** if no function is passed to `thunk`.

@@ -46,6 +47,51 @@ **Example**

**passing custom context**
```js
const tryCatch = require('try-catch-callback')
tryCatch(function () {
console.log(this.foo) // => 'bar'
console.log(this.baz) // => 'qux'
return `${this.foo}/${this.baz}`
}, function done (err, res) {
if (err) return console.error(err)
console.log(res) // => 'bar/qux'
}, {
context: { foo: 'bar', baz: 'qux' }
})
```
**passing custom arguments**
```js
const tryCatchCallback = require('try-catch-callback')
const done = (err, res) => console.log(res)
const opts = {
args: [ { foo: 'zzz' }, 123 ]
}
tryCatchCallback((ctx, qux) => {
return ctx.foo + qux
}, done, opts)
// => 'zzz123'
```
**returning a thunk**
```js
const tryCatch = require('try-catch-callback')
const thunk = tryCatch((a, b) => {
return a + b + 3
}, { args: [1, 2] })
thunk((err, res) => {
console.log(res) // => 6
})
```
## Related
- [catchup](https://www.npmjs.com/package/catchup): Graceful error handling. Because core `domain` module is deprecated. This share almost… [more](https://github.com/tunnckocore/catchup#readme) | [homepage](https://github.com/tunnckocore/catchup#readme "Graceful error handling. Because core `domain` module is deprecated. This share almost the same API.")
- [gana-compile](https://www.npmjs.com/package/gana-compile): Pretty small synchronous template engine built on ES2015 Template Strings, working on… [more](https://github.com/tunnckocore/gana-compile#readme) | [homepage](https://github.com/tunnckocore/gana-compile#readme "Pretty small synchronous template engine built on ES2015 Template Strings, working on `node@0.10` too. No RegExps, support for helpers and what you want. Use [gana][] if you wanna both async and sync support.")
- [gana](https://www.npmjs.com/package/gana): Pretty small synchronous template engine built on es6 template strings, working on… [more](https://github.com/tunnckocore/gana#readme) | [homepage](https://github.com/tunnckocore/gana#readme "Pretty small synchronous template engine built on es6 template strings, working on `node@0.10` too. Just 20 lines of code without RegExps and with support for helpers and what you want.")
- [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][].")

@@ -63,2 +109,5 @@ - [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.")

[dezalgo]: https://github.com/npm/dezalgo
[es6-template]: https://github.com/tunnckocore/es6-template
[gana-compile]: https://github.com/tunnckocore/gana-compile
[gana]: https://github.com/tunnckocore/gana
[once]: https://github.com/isaacs/once

@@ -112,2 +161,1 @@ [try-catch-core]: https://github.com/tunnckocore/try-catch-core

[gana]: https://github.com/tunnckocore/gana
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc