{"id":17483408,"url":"https://github.com/nesterow/grip","last_synced_at":"2025-04-22T14:24:33.774Z","repository":{"id":258125833,"uuid":"873392219","full_name":"nesterow/grip","owner":"nesterow","description":"Simplified result/error handling for JavaScript.","archived":false,"fork":false,"pushed_at":"2024-10-19T14:20:10.000Z","size":38,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-20T07:51:46.483Z","etag":null,"topics":["bunjs","deno","javascript","nodejs","try-catch","typescript"],"latest_commit_sha":null,"homepage":"","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/nesterow.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-16T05:01:32.000Z","updated_at":"2024-10-19T14:20:13.000Z","dependencies_parsed_at":"2024-12-01T16:20:40.748Z","dependency_job_id":"fd1f35eb-6107-45da-8e28-a833785b69cd","html_url":"https://github.com/nesterow/grip","commit_stats":{"total_commits":22,"total_committers":2,"mean_commits":11.0,"dds":"0.045454545454545414","last_synced_commit":"9051b3549182dd777ea0dd5390047c25748c4ecd"},"previous_names":["nesterow/grip"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nesterow%2Fgrip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nesterow%2Fgrip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nesterow%2Fgrip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nesterow%2Fgrip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nesterow","download_url":"https://codeload.github.com/nesterow/grip/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250256851,"owners_count":21400603,"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":["bunjs","deno","javascript","nodejs","try-catch","typescript"],"created_at":"2024-10-19T00:04:50.389Z","updated_at":"2025-04-22T14:24:33.742Z","avatar_url":"https://github.com/nesterow.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Grip\n\n\n[A take on Go Style Error Handling In JavaScript](https://dev.to/nesterow/a-take-on-go-style-error-handling-in-javascript-577)\n\nSimplified result/error handling for JavaScript.\nGrip always returns a consistent call result ready to be handled.\nIt makes the control flow similar to that of Golang, but doesn't force you to make additional null checks or create transitional variables to hold error results.\n\n\u003e Grip doesn't force you to write code in a specific style or avoid using throw. It is just a helper that unwraps try-catch. \n\n\nInstead of returning a nullish error, Grip always returns a consistent result interface:\n\n```javascript\nconst [value, status] = grip(callable) // or {value, status}\n\nif (status.of(MySpecificError)) {\n  // handle specific error\n}\n\nif (status.fail()) {\n  // handle any error\n}\n```\n\nThe call result is better than tuple:\n\n```javascript\nconst result = grip(callable)\n\nif (result.of(MySpecificError)) {\n  // handle specific error\n}\n\nif (result.fail()) {\n  // handle any error\n}\n\nconsole.log(result.value) // result[0]\n```\n\nGrip also works with Promises, functions that return promises, generators and async generators.\n\n## Install\n\n```bash\nbun add github:nesterow/grip # or pnpm\n```\n\n## Usage\n\nThe `grip` function accepts a function or a promise and returns a result with return value and status.\nThe result can be hadled as either an object or a tuple.\n\n```javascript\nimport { grip } from '@nesterow/grip';\n```\n\n## Handle result as an object\n\nThe result can be handled as an object: `{value, status, Ok(), Fail(), Of(type)}`\n\n```javascript\nconst res = await grip(\n  fetch('https://api.example.com')\n);\n\nif (res.fail()) {\n    // handle error\n}\n\nconst json = await grip(\n  res.value.json()\n);\n\nif (json.of(SyntaxError)) {\n    // handle parse error\n}\n\n```\n\n## Handle result as a tuple\n\nThe result can also be received as a tuple if you want to handle errors in Go'ish style:\n\n```javascript\nconst [res, fetchStatus] = await grip(\n  fetch('https://api.example.com')\n);\n\nif (fetchStatus.fail()) {\n    // handle  error\n}\n\nconst [json, parseStatus] = await grip(\n  res.json()\n);\n\nif (parseStatus.of(SyntaxError)) {\n    // handle parse error\n}\n```\n\n## Handle functions\n\nGrip can also handle functions:\n\n```javascript\nconst [result, status] = grip(() =\u003e \"ok\");\n// result = \"ok\"\n\nconst [result1, status1] = grip(() =\u003e {\n  if (1) throw new Error(\"error\")\n});\n// result1 = null\n// status.Of(Error) = true\n```\n\n## Handle generators\n\nGenerators can be handled using the `iter()` method:\n\n```javascript\nconst res = grip(async function* () {\n  for (let i = 0; i \u003c 3; i++) {\n    if (i == 2) throw new Error(\"2\");\n    yield i;\n  }\n});\nfor await (let [value, status] of res.iter()) {\n  if (status.of(Error)) {\n    // handle error properly\n    break;\n  }\n  // typeof value === \"number\"\n  console.log(value)\n}\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnesterow%2Fgrip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnesterow%2Fgrip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnesterow%2Fgrip/lists"}