An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

        


early-return

# 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) {
return

Loading...


}
if (Store.error) {
return

Error!


}
return

Hello World!


}
```

```tsx
function MyComponent() {
early(

Loading...

, Store.loading)
early(

Error!

, Store.error)
return

Hello World!


}
```

Most importantly `early` can also be called in nested methods forcing the execution of all earlier methods to stop as well.