{"id":20604255,"url":"https://github.com/hybridables/try-catch-callback","last_synced_at":"2025-04-15T02:19:59.913Z","repository":{"id":11246981,"uuid":"68918509","full_name":"hybridables/try-catch-callback","owner":"hybridables","description":"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`.","archived":false,"fork":false,"pushed_at":"2022-06-22T08:19:30.000Z","size":372,"stargazers_count":5,"open_issues_count":14,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T12:43:18.168Z","etag":null,"topics":["async","callback","catch","completion","try","try-catch"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hybridables.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-22T12:21:46.000Z","updated_at":"2024-01-18T00:49:04.000Z","dependencies_parsed_at":"2022-09-15T07:13:43.948Z","dependency_job_id":null,"html_url":"https://github.com/hybridables/try-catch-callback","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hybridables%2Ftry-catch-callback","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hybridables%2Ftry-catch-callback/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hybridables%2Ftry-catch-callback/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hybridables%2Ftry-catch-callback/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hybridables","download_url":"https://codeload.github.com/hybridables/try-catch-callback/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248722170,"owners_count":21151211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","callback","catch","completion","try","try-catch"],"created_at":"2024-11-16T09:21:47.472Z","updated_at":"2025-04-15T02:19:59.897Z","avatar_url":"https://github.com/hybridables.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/hybridables\"\u003e\n    \u003cimg height=\"250\" width=\"250\" src=\"https://avatars1.githubusercontent.com/u/10666022?v=3\u0026s=250\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n# try-catch-callback [![NPM version](https://img.shields.io/npm/v/try-catch-callback.svg?style=flat)](https://www.npmjs.com/package/try-catch-callback) [![NPM monthly downloads](https://img.shields.io/npm/dm/try-catch-callback.svg?style=flat)](https://npmjs.org/package/try-catch-callback) [![npm total downloads][downloads-img]][downloads-url]\n\n\u003e 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][].\n\n[![codeclimate][codeclimate-img]][codeclimate-url] \n[![codestyle][standard-img]][standard-url] \n[![linux build][travis-img]][travis-url] \n[![windows build][appveyor-img]][appveyor-url] \n[![codecov][coverage-img]][coverage-url] \n[![dependency status][david-img]][david-url]\n\n## Install\n```\nnpm i try-catch-callback --save\n```\n\n## Usage\n\u003e For more use-cases see the [tests](./test.js)\n\n```js\nconst tryCatchCallback = require('try-catch-callback')\n```\n\n### [tryCatchCallback](index.js#L47)\n\u003e 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][].\n\n**Params**\n\n* `\u003cfn\u003e` **{Function}**: function to be called.    \n* `[opts]` **{Object}**: optional options, such as `context` and `args`    \n* `[opts.context]` **{Object}**: context to be passed to `fn`    \n* `[opts.args]` **{Array}**: custom argument(s) to be pass to `fn`, given value is arrayified    \n* `[opts.passCallback]` **{Boolean}**: pass `true` if you want `cb` to be passed to `fn` args    \n* `[opts.return]` **{Boolean}**: if `true` returns error/value and does not calls `cb`    \n* `[cb]` **{Function}**: callback with `cb(err, res)` signature.    \n* `returns` **{Function}** `thunk`: if `cb` not given.  \n\n**Example**\n\n```js\nvar tryCatch = require('try-catch-callback')\n\ntryCatch(function () {\n  return 'fox qux'\n}, function done (err, res) {\n  if (err) return console.error(err)\n  console.log(res) // =\u003e 'fox qux'\n})\n```\n\n**passing custom context**\n\n```js\nconst tryCatch = require('try-catch-callback')\n\ntryCatch(function () {\n  console.log(this.foo) // =\u003e 'bar'\n  console.log(this.baz) // =\u003e 'qux'\n  return `${this.foo}/${this.baz}`\n}, {\n  context: { foo: 'bar', baz: 'qux' }\n}, function done (err, res) {\n  if (err) return console.error(err)\n  console.log(res) // =\u003e 'bar/qux'\n})\n```\n\n**passing custom arguments**\n\n```js\nconst tryCatchCallback = require('try-catch-callback')\nconst done = (err, res) =\u003e console.log(res) // =\u003e 'zzz123'\nconst opts = {\n  args: [ { foo: 'zzz' }, 123 ]\n}\n\ntryCatchCallback((ctx, qux) =\u003e {\n  return ctx.foo + qux\n}, opts, done)\n\n```\n\n**returning a thunk**\n\n```js\nconst tryCatch = require('try-catch-callback')\nconst thunk = tryCatch((a, b) =\u003e {\n  return a + b + 3\n}, { args: [1, 2] })\n\nthunk((err, res) =\u003e {\n  console.log(res) // =\u003e 6\n})\n```\n\n## Related\n- [catchup](https://www.npmjs.com/package/catchup): Graceful error handling. Because core `domain` module is deprecated. This share almost the same API. | [homepage](https://github.com/tunnckocore/catchup#readme \"Graceful error handling. Because core `domain` module is deprecated. This share almost the same API.\")\n- [gana-compile](https://www.npmjs.com/package/gana-compile): Pretty small synchronous template engine built on ES2015 Template Strings, working on `node@0.10` too. No RegExps, support for helpers and… [more](https://github.com/tunnckocore/gana-compile#readme) | [homepage](https://github.com/tunnckocore/gana-compile#readme \"Pretty small synchronous template engine built on ES2015 Template Strings, working on `node@0.10` too. No RegExps, support for helpers and what you want. Use [gana][] if you wanna both async and sync support.\")\n- [gana](https://www.npmjs.com/package/gana): Small and powerful template engine with only sync and async compile. The mid-level between [es6-template][] and [gana-compile][]. | [homepage](https://github.com/tunnckocore/gana#readme \"Small and powerful template engine with only sync and async compile. The mid-level between [es6-template][] and [gana-compile][].\")\n- [try-catch-core](https://www.npmjs.com/package/try-catch-core): Low-level package to handle completion and errors of sync or asynchronous functions, using [once][] and [dezalgo][] libs. Useful for and… [more](https://github.com/hybridables/try-catch-core#readme) | [homepage](https://github.com/hybridables/try-catch-core#readme \"Low-level package to handle completion and errors of sync or asynchronous functions, using [once][] and [dezalgo][] libs. Useful for and used in higher-level libs such as [always-done][] to handle completion of anything.\")\n- [try-require-please](https://www.npmjs.com/package/try-require-please): Try to require the given module, failing loudly with default message if module does not exists. | [homepage](https://github.com/tunnckocore/try-require-please#readme \"Try to require the given module, failing loudly with default message if module does not exists.\")\n\n## Contributing\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/hybridables/try-catch-callback/issues/new).  \nPlease read the [contributing guidelines](CONTRIBUTING.md) for advice on opening issues, pull requests, and coding standards.  \nIf you need some help and can spent some cash, feel free to [contact me at CodeMentor.io](https://www.codementor.io/tunnckocore?utm_source=github\u0026utm_medium=button\u0026utm_term=tunnckocore\u0026utm_campaign=github) too.\n\n**In short:** If you want to contribute to that project, please follow these things\n\n1. Please DO NOT edit [README.md](README.md), [CHANGELOG.md](CHANGELOG.md) and [.verb.md](.verb.md) files. See [\"Building docs\"](#building-docs) section.\n2. Ensure anything is okey by installing the dependencies and run the tests. See [\"Running tests\"](#running-tests) section.\n3. Always use `npm run commit` to commit changes instead of `git commit`, because it is interactive and user-friendly. It uses [commitizen][] behind the scenes, which follows Conventional Changelog idealogy.\n4. Do NOT bump the version in package.json. For that we use `npm run release`, which is [standard-version][] and follows Conventional Changelog idealogy.\n\nThanks a lot! :)\n\n## Building docs\nDocumentation and that readme is generated using [verb-generate-readme][], which is a [verb][] generator, so you need to install both of them and then run `verb` command like that\n\n```\n$ npm install verbose/verb#dev verb-generate-readme --global \u0026\u0026 verb\n```\n\n_Please don't edit the README directly. Any changes to the readme must be made in [.verb.md](.verb.md)._\n\n## Running tests\nClone repository and run the following in that cloned directory\n\n```\n$ npm install \u0026\u0026 npm test\n```\n\n## Author\n**Charlike Mike Reagent**\n\n+ [github/tunnckoCore](https://github.com/tunnckoCore)\n+ [twitter/tunnckoCore](https://twitter.com/tunnckoCore)\n+ [codementor/tunnckoCore](https://codementor.io/tunnckoCore)\n\n## License\nCopyright © 2016-2017, [Charlike Mike Reagent](http://www.tunnckocore.tk). MIT\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on March 01, 2017._  \n_Project scaffolded using [charlike][] cli._\n\n[dezalgo]: https://github.com/npm/dezalgo\n[es6-template]: https://github.com/tunnckocore/es6-template\n[gana-compile]: https://github.com/tunnckocore/gana-compile\n[gana]: https://github.com/tunnckocore/gana\n[once]: https://github.com/isaacs/once\n[try-catch-core]: https://github.com/hybridables/try-catch-core\n\n[downloads-url]: https://www.npmjs.com/package/try-catch-callback\n[downloads-img]: https://img.shields.io/npm/dt/try-catch-callback.svg\n\n[codeclimate-url]: https://codeclimate.com/github/hybridables/try-catch-callback\n[codeclimate-img]: https://img.shields.io/codeclimate/github/hybridables/try-catch-callback.svg\n\n[travis-url]: https://travis-ci.org/hybridables/try-catch-callback\n[travis-img]: https://img.shields.io/travis/hybridables/try-catch-callback/master.svg?label=linux\n\n[appveyor-url]: https://ci.appveyor.com/project/tunnckoCore/try-catch-callback\n[appveyor-img]: https://img.shields.io/appveyor/ci/tunnckoCore/try-catch-callback/master.svg?label=windows\n\n[coverage-url]: https://codecov.io/gh/hybridables/try-catch-callback\n[coverage-img]: https://img.shields.io/codecov/c/github/hybridables/try-catch-callback/master.svg\n\n[david-url]: https://david-dm.org/hybridables/try-catch-callback\n[david-img]: https://img.shields.io/david/hybridables/try-catch-callback.svg\n\n[standard-url]: https://github.com/feross/standard\n[standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg\n\n[always-done]: https://github.com/hybridables/always-done\n[charlike]: https://github.com/tunnckocore/charlike\n[commitizen]: https://github.com/commitizen/cz-cli\n[standard-version]: https://github.com/conventional-changelog/standard-version\n[verb-generate-readme]: https://github.com/verbose/verb-generate-readme\n[verb]: https://github.com/verbose/verb","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhybridables%2Ftry-catch-callback","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhybridables%2Ftry-catch-callback","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhybridables%2Ftry-catch-callback/lists"}