{"id":48214903,"url":"https://github.com/onderonur/go-try","last_synced_at":"2026-04-04T18:55:49.279Z","repository":{"id":57251721,"uuid":"317013339","full_name":"onderonur/go-try","owner":"onderonur","description":"Easy and flattened error handling for sync and async functions.","archived":false,"fork":false,"pushed_at":"2024-12-14T22:02:13.000Z","size":481,"stargazers_count":17,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-02T19:12:11.043Z","etag":null,"topics":["async-await","error","error-handling","go","golang","try","try-catch","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/onderonur.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":"2020-11-29T18:11:57.000Z","updated_at":"2025-09-25T12:07:15.000Z","dependencies_parsed_at":"2024-03-10T17:30:36.284Z","dependency_job_id":"c0a51725-89f8-46ab-a2dd-cfe3b8aab99c","html_url":"https://github.com/onderonur/go-try","commit_stats":{"total_commits":5,"total_committers":1,"mean_commits":5.0,"dds":0.0,"last_synced_commit":"7e8cb70073018d73814435958f74e864f464ca7f"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/onderonur/go-try","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onderonur%2Fgo-try","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onderonur%2Fgo-try/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onderonur%2Fgo-try/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onderonur%2Fgo-try/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/onderonur","download_url":"https://codeload.github.com/onderonur/go-try/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onderonur%2Fgo-try/sbom","scorecard":{"id":706685,"data":{"date":"2025-08-11","repo":{"name":"github.com/onderonur/go-try","commit":"fa119e6b3e4097638aadb82480d47977faab941d"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.1,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Code-Review","score":0,"reason":"Found 0/8 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/go-try/quality.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/go-try/quality.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:44: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/go-try/quality.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:55: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/go-try/quality.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:66: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/go-try/quality.yml/main?enable=pin","Info:   0 out of   5 GitHub-owned GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/quality.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":6,"reason":"4 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-67mh-4wv8-2f99"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-22T06:48:23.053Z","repository_id":57251721,"created_at":"2025-08-22T06:48:23.054Z","updated_at":"2025-08-22T06:48:23.054Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31409470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-04T10:20:44.708Z","status":"ssl_error","status_checked_at":"2026-04-04T10:20:06.846Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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-await","error","error-handling","go","golang","try","try-catch","typescript"],"created_at":"2026-04-04T18:55:48.757Z","updated_at":"2026-04-04T18:55:49.270Z","avatar_url":"https://github.com/onderonur.png","language":"JavaScript","readme":"# go-try\n\n![Build status](https://img.shields.io/github/actions/workflow/status/onderonur/go-try/quality.yml)\n![License](https://img.shields.io/npm/l/go-try)\n![Version](https://img.shields.io/npm/v/go-try)\n\ngo-try is a zero dependency npm package inspired by [Go](https://golang.org/) programming language (Golang). Its main goal is to reduce use of try-catch blocks in JavaScript/TypeScript code and make exception handling easier with a more flattened structure. It can be used with async or sync functions. It's compatible with both browser and server-side applications.\n\n## Async Example\n\nLet's say that we want to fetch some user and product data from an outer source (like a database, or an API etc) and create an order. To do that, we need to run an some async functions and continue if they succeed. And we need to handle the exceptions, if there are any.  \nWith try-cath blocks, you can do it like this:\n\n```ts\nasync function main() {\n  try {\n    const userId = 123;\n    const user = await getUser(userId);\n    try {\n      const productId = 456;\n      const product = await getProduct(productId);\n      try {\n        const order = await createOrder(user, product);\n        return order;\n      } catch (error) {\n        console.log('Create order failed');\n      }\n    } catch (error) {\n      console.log('Fetch product failed');\n    }\n  } catch (error) {\n    console.log('Fetch user failed');\n  }\n}\n```\n\nIt's handy, but it can create some hard to read, pyramid style code. Sometimes flat code is easier to read and more clear.  \nSo, the code turns into this with go-try:\n\n```ts\nimport { goTry } from 'go-try';\n\nasync function main() {\n  const userId = 123;\n  const [userError, user] = await goTry(() =\u003e getUser(userId));\n  if (userError) {\n    console.log('Get user failed');\n    return;\n  }\n\n  const productId = 456;\n  const [productError, product] = await goTry(() =\u003e getProduct(productId));\n  if (productError) {\n    console.log('Get product failed');\n    return;\n  }\n\n  const [orderError, order] = await goTry(() =\u003e createOrder(user, product));\n  if (orderError) {\n    console.log('Create order failed');\n    return;\n  }\n  return order;\n}\n```\n\n`go` function returns a promise that resolves with an array. The array has 2 items.  \nFirst item is the exception that your async function throws. If the function succeeds, it will be `null`.  \nSecond item is the response of your async function. If the function fails, it will be `null`. Otherwise, it's whatever the async function returns.  \nSo basically, **if `error` is `null`, your async function didn't throw an error.** All we need to do is to check it before running rest of our code.\n\n## Sync Example\n\nEverything above applies for sync functions too. The only difference is, you need to use `goTrySync` instead of `goTry`.\n\n```ts\nimport { goTrySync } from 'go-try';\n\nconst [error, data] = goTrySync(() =\u003e someSyncFunction());\nif (error) {\n  console.log('Failed');\n}\n// Rest of the code\n```\n\n## Destructuring with TypeScript\n\nIf you are using a `typescript` version below `v4.6.0`, you will come accross a ts error like below:\n\n```ts\nconst [error, data] = await go(() =\u003e getUser());\nif (error) {\n  console.log('Failed');\n  return;\n}\n\n// At this point, TypeScript (if the version is below `4.6.0`) can't understand because the `error` is `null`,\n// `data` should be the type of what's resolved by promise returned by `getUser` function.\n// TypeScript still thinks `data` can be `null`.\n// We need an extra check like:\nif (!data) {\n  return;\n}\n```\n\nYou can check [here](https://devblogs.microsoft.com/typescript/announcing-typescript-4-6/#control-flow-analysis-for-destructured-discriminated-unions) for more information.\n\n## With TypeScript\n\nBasically, you don't need to specify type of your returned `data`. `goTry` and `goTrySync` are generic functions. So, they can understand what is your data type by themselves. But if you want to explicitly specify your `data` and `error` type, you can do that too.  \nFor example:\n\n```ts\n// Let's say that your function returns an array of some users.\n// And it throws a custom error instance of a class that extends Error\n// (like a NotFoundError, AxiosError etc)\ninterface User {\n  id: string;\n  firstName: string;\n  lastName: string;\n}\n\n// ...\nconst [error, users] = await goTry\u003cUser[], NotFoundError\u003e(() =\u003e getUsers());\n// or\nconst [error, users] = goTrySync\u003cUser[], NotFoundError\u003e(() =\u003e getUsersSync());\n\n// Or you can just specify your data type if your error is a classic Error instance\nconst [error, users] = await goTry\u003cUser[]\u003e(() =\u003e getUsers());\n// or\nconst [error, users] = goTrySync\u003cUser[]\u003e(() =\u003e getUsersSync());\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonderonur%2Fgo-try","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonderonur%2Fgo-try","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonderonur%2Fgo-try/lists"}