{"id":17234837,"url":"https://github.com/astoilkov/good-try","last_synced_at":"2025-04-14T02:07:25.470Z","repository":{"id":46786263,"uuid":"509797290","full_name":"astoilkov/good-try","owner":"astoilkov","description":"Tries to execute a sync/async function, returns a specified default value if the function throws","archived":false,"fork":false,"pushed_at":"2022-12-19T13:04:48.000Z","size":29,"stargazers_count":18,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T02:07:06.498Z","etag":null,"topics":["error-handling","errors","nice-try","try-catch"],"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/astoilkov.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}},"created_at":"2022-07-02T15:52:51.000Z","updated_at":"2025-03-18T12:58:11.000Z","dependencies_parsed_at":"2023-01-29T21:30:29.187Z","dependency_job_id":null,"html_url":"https://github.com/astoilkov/good-try","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astoilkov%2Fgood-try","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astoilkov%2Fgood-try/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astoilkov%2Fgood-try/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astoilkov%2Fgood-try/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/astoilkov","download_url":"https://codeload.github.com/astoilkov/good-try/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248809044,"owners_count":21164896,"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","errors","nice-try","try-catch"],"created_at":"2024-10-15T05:30:49.780Z","updated_at":"2025-04-14T02:07:25.432Z","avatar_url":"https://github.com/astoilkov.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# `good-try`\n\n\u003e Tries to execute a sync/async function, returns a specified default value if the function throws.\n\n[![Gzipped Size](https://img.shields.io/bundlephobia/minzip/good-try)](https://bundlephobia.com/result?p=good-try)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/astoilkov/good-try/main.yml?branch=main)](https://github.com/astoilkov/good-try/actions/workflows/main.yml)\n\n## Why\n\nWhy not [`nice-try`](https://github.com/electerious/nice-try) with it's 70+ million downloads per month?\n- `good-try` supports async functions.\n- `good-try` supports an optional default value.\n- `good-try` allows you to capture the thrown error.\n- `good-try` is written in TypeScript. The types are written in a way that reduce developer errors. For example, I sometimes incorrectly type `goodTry(readFileSync())`, but the types don't allow this.\n- `good-try` has a friend — [`settle-it`](https://github.com/astoilkov/settle-it).\n- I aim for high-quality with [my open-source principles](https://astoilkov.com/my-open-source-principles).\n\nWhy not just `try`/`catch`?\n- In a lot of cases, `try`/`catch` is still the better option.\n- Nested `try`/`catch` statements are hard to process mentally. They also indent the code and make it hard to read. A single `try`/`catch` does the same but to a lesser degree.\n- If you [prefer const](https://eslint.org/docs/latest/rules/prefer-const), `try`/`catch` statements get in the way because you need to use `let` if you need the variable outside of the `try`/`catch` scope:\n  ```ts\n  let todos;\n  try {\n      todos = JSON.parse(localStorage.getItem('todos'))\n  } catch {}\n  return todos.filter(todo =\u003e todo.done)\n  ```\n- It takes more space. It's slower to type.\n\n## Install\n\n```bash\nnpm install good-try\n```\n\n## Usage\n\n```ts\nimport goodTry from 'good-try'\n\n// tries to parse todos, returns empty array if it fails\nconst value = goodTry(() =\u003e JSON.parse(todos), [])\n\n// fetch todos, on error, fallback to empty array\nconst todos = await goodTry(fetchTodos(), [])\n\n// fetch todos, fallback to empty array, send error to your error tracking service\nconst todos = await goodTry(fetchTodos(), (err) =\u003e {\n    sentToErrorTrackingService(err)\n    return []  \n})\n```\n\n## API\n\n**First parameter** accepts:\n- synchronous function `goodTry(() =\u003e JSON.parse(value))`\n- asynchronous function / Promise\n- synchronous function that returns a promise\n\n**Second parameter** accepts:\n- any value that will be returned if the first parameter throws\n- a callback that receives `err` as first parameter (the return value of the callback is returned if the first parameter throws)\n\nIf you use TypeScript, the types are well defined and won't let you make a mistake.\n\n## Related\n\n- [settle-it](https://github.com/astoilkov/settle-it) – Like `Promise.allSettled()` but for sync and async functions. Similarly to `good-try` it handles sync/async functions that throw an error. However, it returns an object so you know if and what error was thrown.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastoilkov%2Fgood-try","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastoilkov%2Fgood-try","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastoilkov%2Fgood-try/lists"}