{"id":21028234,"url":"https://github.com/ahme-dev/tryresult","last_synced_at":"2025-05-15T10:33:11.040Z","repository":{"id":166766597,"uuid":"642275348","full_name":"ahme-dev/tryresult","owner":"ahme-dev","description":"📛 Simple typescript library to get rid of try catches.","archived":false,"fork":false,"pushed_at":"2023-06-20T20:02:05.000Z","size":118,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-15T11:39:40.711Z","etag":null,"topics":["error-handling","library","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/tryresult","language":"TypeScript","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/ahme-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-05-18T07:51:49.000Z","updated_at":"2023-06-28T09:26:47.000Z","dependencies_parsed_at":"2023-11-17T08:06:18.575Z","dependency_job_id":null,"html_url":"https://github.com/ahme-dev/tryresult","commit_stats":null,"previous_names":["ahmeddots/faults","ahme-dev/tryresult","ahmeddots/tryresult"],"tags_count":12,"template":false,"template_full_name":"egoist/ts-lib-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahme-dev%2Ftryresult","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahme-dev%2Ftryresult/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahme-dev%2Ftryresult/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahme-dev%2Ftryresult/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahme-dev","download_url":"https://codeload.github.com/ahme-dev/tryresult/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254323254,"owners_count":22051747,"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":["error-handling","library","typescript"],"created_at":"2024-11-19T11:54:32.450Z","updated_at":"2025-05-15T10:33:10.629Z","avatar_url":"https://github.com/ahme-dev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/ahmeddots/tryresult/ci.yaml)\n![NPM bundle size](https://img.shields.io/bundlephobia/min/tryresult?color=royalblue)\n![npm](https://img.shields.io/npm/v/tryresult?label=version\u0026color=royalblue)\n![npm](https://img.shields.io/npm/dm/tryresult?color=gold)\n\n# 📛 TryResult\n\nA typescript library to get rid of try catches, and replace them with result types, inspired by Rust and Go error handling.\n\n![preview picture](./preview.jpg)\n\nProviding simple, easier, and more elegeant error handling, TryResult gives you functions that act as wrappers and catch errors in your own functions.\n\nIt also currently provides functions to assert if a result has an error in it, and to use a default value in case of errors.\n\n\u003cbr\u003e\n\n## Install\n\nAs with any npm package:\n\n```sh\nnpm i tryresult\n```\n\nOr use Yarn:\n\n```sh\nyarn add tryresult\n```\n\n\u003cbr\u003e\n\n## Usage\n\nImport from the package:\n\n```typescript\nimport { tryAsync, isError } from \"tryresult\";\n```\n\nWrap your async function with `tryAsync`:\n\n```typescript\nlet users = await tryAsync(\n\t// get a list of users from the database\n\tdb.user.findMany(),\n);\n```\n\nThis will make the `users` variable be of type `T | Error`, meaning it can be either a value or an error (a union of types).\n\nThen check for error in the variable with `isError`, and then handle the error:\n\n```typescript\nif (isError(users)) {\n\treturn \"Could not get users from db\";\n}\n```\n\nThis is a type guard and all code after the `isError` will consider result's type to be `T`.\n\n**[v1.2.x onwards]**\n\nLet's say you're fetching something like a user's role from the db:\n\n```typescript\nconst result = await tryAsync(db.user.find(id).role);\n```\n\nIf you want to get the value and set a default value in case of error, you can use `okOr` on the result:\n\n```typescript\nconst role = okOr(result, \"guestUser\");\n```\n\nNow `role` is gonna be either the value from the db, or if there was an error, `\"guestUser\"`.\n\n\u003cbr\u003e\n\nTo see the library used in a project, checkout out [ahmeddots/oswald](https://github.com/ahmeddots/oswald).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahme-dev%2Ftryresult","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahme-dev%2Ftryresult","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahme-dev%2Ftryresult/lists"}