Socket
Socket
Sign inDemoInstall

roadblock

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    roadblock

A simple function for interrupting program flow if a condition isn’t met.


Version published
Weekly downloads
1.6K
decreased by-58.38%
Maintainers
1
Install size
4.22 kB
Created
Weekly downloads
 

Readme

Source

roadblock

A simple function for interrupting program flow if a condition isn’t met.

Installation

Requires Node.js 4.0.0 or above.

npm i roadblock

API

The module exposes a single function:

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

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:

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.

Keywords

FAQs

Last updated on 03 Nov 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc