early-return
v0.1.1
Published
Helper to facilitate early returns across function stack in JavaScript.
Downloads
23
Readme
early-return
Helper to facilitate early returns across function stack in JavaScript.
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.
function MyComponent() {
if (Store.loading) {
return <p>Loading...</p>
}
if (Store.error) {
return <p>Error!</p>
}
return <p>Hello World!</p>
}
function MyComponent() {
early(<p>Loading...</p>, Store.loading)
early(<p>Error!</p>, Store.error)
return <p>Hello World!</p>
}
Most importantly early
can also be called in nested methods forcing the execution of all earlier methods to stop as well.