https://github.com/tobua/early-return
Helper to facilitate early returns across function stack in JavaScript.
https://github.com/tobua/early-return
Last synced: 20 days ago
JSON representation
Helper to facilitate early returns across function stack in JavaScript.
- Host: GitHub
- URL: https://github.com/tobua/early-return
- Owner: tobua
- Created: 2024-11-19T15:57:31.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-03-13T13:29:42.000Z (3 months ago)
- Last Synced: 2025-05-08T01:16:02.611Z (20 days ago)
- Language: TypeScript
- Homepage: https://npmjs.com/early-return
- Size: 69.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
![]()
# early-return
Helper to facilitate early returns across function stack in JavaScript.
```ts
import { early, earlyReturn } from 'early-return'function handler() {
early(1, true) // Will return, value 1.
early(2, false) // Will be skipped, no value.
early(3) // Will return, value 3.
}const value = earlyReturn(handler)
```This plugin is especially useful when integrated directly into frameworks, so that the overhead of wrapping functions isn't necessary. The following example shows how it simplifies JSX components.
```tsx
function MyComponent() {
if (Store.loading) {
returnLoading...
}
if (Store.error) {
returnError!
}
returnHello World!
}
``````tsx
function MyComponent() {
early(Loading...
, Store.loading)
early(Error!
, Store.error)
returnHello World!
}
```Most importantly `early` can also be called in nested methods forcing the execution of all earlier methods to stop as well.