JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 97
  • Score
    100M100P100Q79227F
  • License MIT

Asynchronous and sync tryCatch in one place. The callback is securely wrapped with a [dezalgo][] and [once][].

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 npmjs.com The MIT License npm downloads

Asynchronous and sync tryCatch in one place. The callback is securely wrapped with a dezalgo and once.

code climate standard code style travis build status coverage status dependency status

Install

npm i try-catch-core --save

Usage

For more use-cases see the tests

const tryCatchCore = require('try-catch-core')

tryCatchCore

Executes given fn and pass results/errors to the callback if 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 used
  • returns {Function} thunk: only if cb is 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'
})

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.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github