https://github.com/lamansky/roadblock
[Node.js] A simple function for interrupting program flow if a condition isn’t met
https://github.com/lamansky/roadblock
control-flow javascript nodejs
Last synced: 11 months ago
JSON representation
[Node.js] A simple function for interrupting program flow if a condition isn’t met
- Host: GitHub
- URL: https://github.com/lamansky/roadblock
- Owner: lamansky
- License: mit
- Created: 2017-03-06T15:26:58.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-03T15:20:31.000Z (over 7 years ago)
- Last Synced: 2025-08-09T15:32:53.738Z (11 months ago)
- Topics: control-flow, javascript, nodejs
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# roadblock
A simple function for interrupting program flow if a condition isn’t met.
## Installation
Requires [Node.js](https://nodejs.org/) 4.0.0 or above.
```bash
npm i roadblock
```
## API
The module exposes a single function:
```javascript
module.exports = (shouldBlock, block, main) => shouldBlock ? block(main) : main()
```
If `shouldBlock` is true, then the `block` function is called and is given `main` as an argument, to be invoked when/if `block` is ready. If `shouldBlock` is false, `block` is bypassed and `main` is called immediately.
## Example
```javascript
const roadblock = require('roadblock')
roadblock(!loggedIn, main => {
// show login form
if (loginSuccessful) main()
}, () => {
// show user-only page
})
```
In the above example, if `loggedIn` is false, the login function is invoked, and is given a callback which it can use once the login is successful. If, on the other hand, `loggedIn` is true, then the login function is bypassed and the main function is called directly.
### The Alternative
Without `roadblock`, the code for the previous example would look something like this:
```javascript
function login () {
// show login form
if (loginSuccessful) main()
}
function main () {
// show user-only page
}
if (loggedIn) {
main()
} else {
login()
}
```
`roadblock` is intended to make your code more compact, more sequential, and less cluttered with named functions that are used only once or twice.