{"id":17234835,"url":"https://github.com/astoilkov/settle-it","last_synced_at":"2025-04-14T02:07:46.897Z","repository":{"id":46786841,"uuid":"510042504","full_name":"astoilkov/settle-it","owner":"astoilkov","description":"Deal with code that can throw","archived":false,"fork":false,"pushed_at":"2023-11-08T13:10:31.000Z","size":33,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T02:07:42.237Z","etag":null,"topics":["error-handling","errors","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,"governance":null}},"created_at":"2022-07-03T14:13:36.000Z","updated_at":"2024-11-20T16:23:34.000Z","dependencies_parsed_at":"2023-01-29T21:30:34.568Z","dependency_job_id":"84f26d32-1676-4196-9b59-79af4472233d","html_url":"https://github.com/astoilkov/settle-it","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"fba29aa38e0618643d809006f4f8149dd462393e"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astoilkov%2Fsettle-it","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astoilkov%2Fsettle-it/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astoilkov%2Fsettle-it/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astoilkov%2Fsettle-it/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/astoilkov","download_url":"https://codeload.github.com/astoilkov/settle-it/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","try-catch"],"created_at":"2024-10-15T05:30:49.188Z","updated_at":"2025-04-14T02:07:46.873Z","avatar_url":"https://github.com/astoilkov.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `settle-it`\n\n\u003e Deal with code that can throw.\n\n[![Gzipped Size](https://img.shields.io/bundlephobia/minzip/settle-it)](https://bundlephobia.com/result?p=settle-it)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/astoilkov/settle-it/main.yml?branch=main)](https://github.com/astoilkov/settle-it/actions/workflows/main.yml)\n\n## Install\n\n```bash\nnpm install settle-it\n```\n\n## Why\n\nMost commonly I use `try/catch`, but in some cases it's great:\n- if you prefer a one-liner\n- when you want to ignore the error\n- to avoid nesting `try/catch` statements\n- when 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\nAlso:\n- `err` is always an `Error` object — great for TypeScript \u0026 unexpected cases when someone `throw 'error'`\n- Supports a `fallback` value.\n- `fallback` can be a function that accepts the `Error` — great for working with it\n\n## Examples\n\nSafely parse JSON \u0026 specify the type (one-liner):\n```ts\nimport settle from 'settle-it'\n\nconst [parsed] = settle\u003cState\u003e(() =\u003e JSON.parse(value))\n// parsed is State | undefined\n```\n\nPrefer `const`:\n```ts\nconst isOnline = settle(async () =\u003e {\n    const response = await fetch('https://status.com/check')\n    const json = response.json()\n    return json.isOnline\n}, false)\n```\n\nSafely fetch \u0026 on error, send to error tracking service (one-liner):\n```ts\nimport settle from 'settle-it'\n\nconst [response] = await settle(fetch('https://todos.com/get'), sendToErrorTrackingService)\n// response is Response | undefined\n```\n\nSafely read a file \u0026 fallback to empty string (one-liner):\n```ts\nimport { readFile } from 'node:fs/promises'\n\nconst [content] = await settle(readFile(path), '')\n// content is string\n```\n\nAvoid nesting `try/catch` statements:\n```ts\nconst user = settle(() =\u003e JSON.parse(json), () =\u003e showDialog('failed to parse'))\nconst contents = settle(fetch(`http://example.com/${user.id}`), () =\u003e showDialog('failed to fetch'))\n\n// to show different errors to the user you need to nest try/catch statements\n```\n\n## API\n\nI usually prefer [source code](./index.ts) or examples ↑.\n\n```ts\nsettle\u003cT, F\u003e(\n    value: Promise | (() =\u003e T) | (() =\u003e Promise\u003cT\u003e),\n    fallback: F | ((err: Error) =\u003e F | void)\n): [T, undefined] | [F, Error]\n```\n\n- **First parameter** accepts either sync/async function or a Promise.\n- **Returns** `[value, undefined]` if no error was thrown while executing the function.\n- **Returns** `[fallback, Error]` is an error was thrown.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastoilkov%2Fsettle-it","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastoilkov%2Fsettle-it","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastoilkov%2Fsettle-it/lists"}