Package Exports
- try-catch-core
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-core) 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-core

Asynchronous and sync tryCatch in one place. The callback is securely wrapped with a dezalgo and once.
Install
npm i try-catch-core --saveUsage
For more use-cases see the tests
const tryCatchCore = require('try-catch-core')tryCatchCore
Executes given
fnand pass results/errors to thecallbackif given, otherwise returns a thunk.
Why this can be useful?
1) Because this can be used to handle completion
and errors of anything like Observable, Promises,
Streams, Child Processes and Synchronous functions.
2) Can also be tricked the fn to accept generator
functions, so later you can just yield what you want.
3) Or what about to be used as "thunkify" lib?
Params
<fn>{Function}: function to call[cb]{Function}: done callback to be usedreturns{Function}thunk: only ifcbis not a function
Example
var fs = require('fs')
var tryCatch = require('try-catch-core')
// successful synchronous handling
tryCatch(function () {
return 'foo bar'
}, function done (err, res) {
console.log(err) // => null
console.log(res) // => 'foo bar'
})
// failing sync handling
tryCatch(function () {
throw new Error('qux baz')
}, function done (err) {
console.log(err) // => Error: qux baz
})
// async error handling
tryCatch(function (done) {
fs.readFile('not-existing', done)
}, function done (err) {
console.log(err)
// => Error: ENOENT, no such file or directory
})
// successful async handling
tryCatch(function (done) {
fs.readFile('./package.json', 'utf-8', done)
}, function done (err, str) {
console.log(err) // => null
console.log(JSON.parse(str).name) // => 'try-catch-core'
})
// returning thunk
var thunk = tryCatch(function () {
return JSON.parse('{"foo":"bar qux"}')
})
thunk(function done (err, obj) {
console.log(err) // => null
console.log(obj.foo) // => 'bar qux'
})Related
- function-arguments: Get arguments of a function, useful for and used in dependency injectors… more | homepage
- gana-compile: Pretty small synchronous template engine built on ES2015 Template Strings, working on… more | homepage
- is-async-function: Is function really asynchronous function? Trying to guess that based on check… more | homepage
- relike: Simple promisify async or sync function with sane defaults. Lower level than… 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.