{"id":18897286,"url":"https://github.com/iagocalazans/try2catch","last_synced_at":"2026-06-07T02:03:50.164Z","repository":{"id":153735280,"uuid":"605987371","full_name":"iagocalazans/try2catch","owner":"iagocalazans","description":"A better try/catch like way to get your errors encapsulated.","archived":false,"fork":false,"pushed_at":"2026-06-06T22:44:54.000Z","size":52,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-06-07T00:13:26.421Z","etag":null,"topics":["async","catch","promise","try","trycatch"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/try2catch","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iagocalazans.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-24T10:43:18.000Z","updated_at":"2026-06-06T22:44:27.000Z","dependencies_parsed_at":"2023-09-03T03:04:17.328Z","dependency_job_id":null,"html_url":"https://github.com/iagocalazans/try2catch","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/iagocalazans/try2catch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iagocalazans%2Ftry2catch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iagocalazans%2Ftry2catch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iagocalazans%2Ftry2catch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iagocalazans%2Ftry2catch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iagocalazans","download_url":"https://codeload.github.com/iagocalazans/try2catch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iagocalazans%2Ftry2catch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34006048,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-07T02:00:07.652Z","response_time":124,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","catch","promise","try","trycatch"],"created_at":"2024-11-08T08:36:52.711Z","updated_at":"2026-06-07T02:03:50.159Z","avatar_url":"https://github.com/iagocalazans.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Try2Catch\n\nA better try/catch like way to get your errors encapsulated.\n\n## Installation\n\nYou can install Try2Catch using `npm` or `yarn`:\n\n```bash\nnpm install try2catch\n```\n\n```bash\nyarn add try2catch\n```\n\n## Usage\n\nTo use Try2Catch, import the Try object from the package:\n\n```javascript\nimport { Try } from 'try2catch';\n```\n\nCreate a Promise that resolves or rejects after some time, for example:\n\n```javascript\nconst awaiter = new Promise((res, reject) =\u003e {\n    const random = (Math.random() * 10) + 1\n    setTimeout(() =\u003e { \n            if (random \u003e 5) {\n                return res('Nice!');\n            }\n        \n            return reject(new Error('Failed!'));\n        }, 5000);\n});\n```\n\nAn asynchronous function that uses Try to handle the Promise's result:\n\n```javascript\nconst processing = async () =\u003e {\n    const [data, error] = await Try.promise(awaiter)\n\n    if (error) {\n        return error;\n    }\n\n    return data\n}\n```\n\nIn this example, `Try.promise()` takes a Promise as an argument and returns a positional tuple `[data, error]`. If the Promise resolves successfully, position 0 holds the resolved value and position 1 is `null`. If the Promise is rejected, position 0 is `null` and position 1 holds the rejection reason. Exactly one of the two positions is non-null on every settlement.\n\n### Settling many Promises\n\n`Try.settled()` runs an array of Promises in parallel and partitions the outcomes without ever rejecting, returning a `[values, reasons]` tuple:\n\n```javascript\nconst [values, reasons] = await Try.settled([fetchA(), fetchB(), fetchC()]);\n```\n\n`values` collects every resolved settlement and `reasons` collects every rejection, each as an `[index, value]` (or `[index, reason]`) pair. The index is the original position of the Promise in the input array, so an outcome can always be traced back to the call that produced it:\n\n```javascript\nconst [values, reasons] = await Try.settled([fetchA(), fetchB(), fetchC()]);\n\nfor (const [index, value] of values) {\n  console.log(`Promise ${index} resolved with`, value);\n}\n\nfor (const [index, reason] of reasons) {\n  console.error(`Promise ${index} rejected with`, reason);\n}\n```\n\n### Inspecting types\n\n`Try.typeOf()` returns the runtime type tag of any value:\n\n```javascript\nTry.typeOf([]);        // 'Array'\nTry.typeOf(null);      // 'Null'\nTry.typeOf(new Map()); // 'Map'\n```\n\n## Migrating from 1.x to 2.0\n\n\u003e **Breaking change:** `Try.promise()` no longer returns a wrapper object. It now returns a positional tuple `[data, error]`. The instance helpers `isError()`, `data`, and `error` were removed, along with the internal `TryThen` / `TryCatch` classes (which were never exported).\n\n### What changed\n\n- **Return shape:** `Try.promise()` returned a `TryThen` / `TryCatch` object in 1.x; in 2.0 it returns a `[data, error]` tuple.\n- **Discrimination:** `result.isError()` is gone; check the second position instead with `if (error)`.\n- **Success value:** `result.data` becomes the first position of the tuple.\n- **Failure value:** `result.error` becomes the second position of the tuple.\n- **New helper:** 2.0 adds `Try.settled()` for handling arrays of Promises in parallel.\n\n### How to migrate\n\nReplace the wrapper-object access with tuple destructuring.\n\n**Before (1.x):**\n\n```javascript\nconst result = await Try.promise(awaiter);\n\nif (result.isError()) {\n    return result.error;\n}\n\nreturn result.data;\n```\n\n**After (2.0):**\n\n```javascript\nconst [data, error] = await Try.promise(awaiter);\n\nif (error) {\n    return error;\n}\n\nreturn data;\n```\n\n### Migration tips\n\n- **Find every call site.** Search your codebase for `Try.promise` and for the `.isError()`, `.data`, and `.error` accessors that follow it.\n- **Pick your variable names per call.** The tuple is positional, not named, so rename freely: `const [user, userError] = await Try.promise(...)`.\n- **Mind TypeScript narrowing when destructuring.** Checking `error` does not narrow `data` to non-null, because they become independent variables. When you need `data` typed as non-null after an early return, assert it (`data!`) or guard with `if (!error)`.\n- **Drop any imports of `TryThen` / `TryCatch`.** They were internal and are gone in 2.0; only `Try` is part of the public API.\n\n## License\n\nThis package is licensed under the MIT license. See the LICENSE file for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiagocalazans%2Ftry2catch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiagocalazans%2Ftry2catch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiagocalazans%2Ftry2catch/lists"}