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

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

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
379
increased by42.48%
Maintainers
1
Weekly downloads
 
Created
Source

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.
  • [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.
  • [cb] {Function}: callback with cb(err, res) signature.
  • 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}`
}, {
  context: { foo: 'bar', baz: 'qux' }
}, function done (err, res) {
  if (err) return console.error(err)
  console.log(res) // => 'bar/qux'
})

passing custom arguments

const tryCatchCallback = require('try-catch-callback')
const done = (err, res) => console.log(res) // => 'zzz123'
const opts = {
  args: [ { foo: 'zzz' }, 123 ]
}

tryCatchCallback((ctx, qux) => {
  return ctx.foo + qux
}, opts, done)

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

Keywords

FAQs

Package last updated on 01 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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