{"id":18552471,"url":"https://github.com/andrejewski/throw-return","last_synced_at":"2025-08-02T10:33:59.515Z","repository":{"id":68511823,"uuid":"155094595","full_name":"andrejewski/throw-return","owner":"andrejewski","description":"Return using throw","archived":false,"fork":false,"pushed_at":"2020-05-11T17:16:34.000Z","size":40,"stargazers_count":3,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-16T17:52:08.486Z","etag":null,"topics":[],"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/andrejewski.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":"2018-10-28T17:03:54.000Z","updated_at":"2019-04-25T15:27:50.000Z","dependencies_parsed_at":"2023-02-22T16:45:13.874Z","dependency_job_id":null,"html_url":"https://github.com/andrejewski/throw-return","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"9d6a23ea495c701821d0cd136c16cdfe0f99ca57"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/andrejewski/throw-return","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fthrow-return","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fthrow-return/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fthrow-return/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fthrow-return/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrejewski","download_url":"https://codeload.github.com/andrejewski/throw-return/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fthrow-return/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268371654,"owners_count":24239793,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2024-11-06T21:14:19.959Z","updated_at":"2025-08-02T10:33:59.490Z","avatar_url":"https://github.com/andrejewski.png","language":"JavaScript","readme":"# throw-return\n\u003e Return using throw\n\n```sh\nnpm install throw-return\n```\n\n[![npm](https://img.shields.io/npm/v/throw-return.svg)](https://www.npmjs.com/package/throw-return)\n[![Build Status](https://travis-ci.org/andrejewski/throw-return.svg?branch=master)](https://travis-ci.org/andrejewski/throw-return)\n[![Greenkeeper badge](https://badges.greenkeeper.io/andrejewski/throw-return.svg)](https://greenkeeper.io/)\n\nThrow errors to communicate a return value.\n\n**Warning:**\nEvaluate your use cases before using this library.\nUsing error handling to short-circuit functions is slower and more obscure than returning.\nThis library should be used as part of a larger abstraction.\nUnless that abstraction provides more benefits in itself, go with plain `return`.\n\n## Documentation\n\n- [`throwReturnError(value[, message])`](#throwreturnerror)\n- [`handleReturnError(func)`](#handlereturnerror)\n\n### `throwReturnError`\n\u003e `throwReturnError(value: any[, message: String])`\n\nThrow an error communicating `value` as the return value.\nOptionally give the error a `message`.\n\n### `handleReturnError`\n\u003e `handleReturnError(func: Function): any`\n\nInvoke `func` and, if a `throw-return` error is thrown, return the communicated value.\nIf a promise is returned rejecting a `throw-error` error, the communicated value is resolved.\n\n## Example\nWe have found `if (!y) return x` too tiresome to write.\nLet's build an `assert(y, x)` to save some keystrokes.\n\nInstead of this:\n\n```js\nfunction example () {\n  if (!condition1) {\n    return value1\n  }\n  if (!condition2) {\n    return value2\n  }\n  return value3\n}\n```\n\nWe can do:\n\n```js\nfunction example () {\n  assert(condition1, value1)\n  assert(condition2, value2)\n  return value3\n}\n```\n\nIf you define `assert` and control all calls to `example`:\n\n```js\nfunction assert (condition, value) {\n  if (!condition) {\n    throwReturnError(value)\n  }\n}\n\nfunction run (func) {\n  return handleReturnError(func)\n}\n\nconst value = run(example)\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrejewski%2Fthrow-return","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrejewski%2Fthrow-return","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrejewski%2Fthrow-return/lists"}