https://github.com/andrejewski/throw-return
Return using throw
https://github.com/andrejewski/throw-return
Last synced: 6 months ago
JSON representation
Return using throw
- Host: GitHub
- URL: https://github.com/andrejewski/throw-return
- Owner: andrejewski
- License: mit
- Created: 2018-10-28T17:03:54.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-11T17:16:34.000Z (over 5 years ago)
- Last Synced: 2025-06-16T17:52:08.486Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 39.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# throw-return
> Return using throw
```sh
npm install throw-return
```
[](https://www.npmjs.com/package/throw-return)
[](https://travis-ci.org/andrejewski/throw-return)
[](https://greenkeeper.io/)
Throw errors to communicate a return value.
**Warning:**
Evaluate your use cases before using this library.
Using error handling to short-circuit functions is slower and more obscure than returning.
This library should be used as part of a larger abstraction.
Unless that abstraction provides more benefits in itself, go with plain `return`.
## Documentation
- [`throwReturnError(value[, message])`](#throwreturnerror)
- [`handleReturnError(func)`](#handlereturnerror)
### `throwReturnError`
> `throwReturnError(value: any[, message: String])`
Throw an error communicating `value` as the return value.
Optionally give the error a `message`.
### `handleReturnError`
> `handleReturnError(func: Function): any`
Invoke `func` and, if a `throw-return` error is thrown, return the communicated value.
If a promise is returned rejecting a `throw-error` error, the communicated value is resolved.
## Example
We have found `if (!y) return x` too tiresome to write.
Let's build an `assert(y, x)` to save some keystrokes.
Instead of this:
```js
function example () {
if (!condition1) {
return value1
}
if (!condition2) {
return value2
}
return value3
}
```
We can do:
```js
function example () {
assert(condition1, value1)
assert(condition2, value2)
return value3
}
```
If you define `assert` and control all calls to `example`:
```js
function assert (condition, value) {
if (!condition) {
throwReturnError(value)
}
}
function run (func) {
return handleReturnError(func)
}
const value = run(example)
```