JSPM

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

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][].

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

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.

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

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 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.

Params

  • <fn> {Function}: function to be called.
  • [cb] {Function}: callback with cb(err, res) signature.
  • [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.

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
})

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