Package Exports
- try-catch-callback
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (try-catch-callback) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
try-catch-callback

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.
Install
npm i try-catch-callback --save
Usage
For more use-cases see the tests
const tryCatchCallback = require('try-catch-callback')
tryCatchCallback
Pass a synchronous
fn
that returns some result and handle completion or errors incb
if given, otherwise it returns thunk which accepts thatcb
. 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 thatcb
is called only once and always in next tick, using dezalgo and once.
Params
<fn>
{Function}: function to be called.[cb]
{Function}: callback withcb(err, res)
signature.[opts]
{Object}: optional options, such ascontext
andargs
[opts.context]
{Object}: context to be passed tofn
[opts.args]
{Array}: custom argument(s) to be pass tofn
, given value is arrayified[opts.passCallback]
{Boolean}: passtrue
if you wantcb
to be passed tofn
args.returns
{Function}thunk
: ifcb
not given.
Example
var tryCatch = require('try-catch-callback')
tryCatch(function () {
return 'fox qux'
}, function done (err, res) {
if (err) return console.error(err)
console.log(res) // => 'fox qux'
})
passing custom context
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
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
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: Graceful error handling. Because core
domain
module is deprecated. This share almost… more | homepage - gana-compile: Pretty small synchronous template engine built on ES2015 Template Strings, working on… more | homepage
- gana: Small and powerful template engine with only sync and async compile. The… more | homepage
- try-catch-core: Asynchronous and sync tryCatch in one place. The callback is securely wrapped… more | homepage
- try-require-please: Try to require the given module, failing loudly with default message if… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.