Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

call-reduce

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

call-reduce

Reduce an array of functions, calling the next explicitly

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

CallReduce

Iterate through a sequence of methods, firing each method explicitly in turn with a next callback.

Useful for asynchronous pipes or middleware.

Installation

npm install call-reduce --save or yarn add call-reduce

Basic Usage

const callReduce = require('call-reduce')
const stack = callReduce(
    (x, next) => next(x + ' world!'),
    (y, next) => {
        console.log(y) // hello world!
    },
    z => {
        throw new Error("We shouldn't get here since `next` is never called at step 2")
    }
)
stack('hello')

Advanced Asynchronous Example

Imagine your Node application has some request middleware that looks up a user from the DB and determines if they have permissions to access that route, or whether they should be rejected.

While a bit more advanced, you could also pipe the req and res through a whole series of asynchronous operations easily using this pattern, composing each as a stand-alone method

const { createServer } = require('http')
const callReduce = require('call-reduce')
const auth = callReduce(
    (req, res, next) => {
        User.findOne({_id: req.params.user}, (err, user) => {
            if (user.canAccess(req)) {
                next(req, res)
            } else {
                res.statusCode = 403
                res.end('Forbidden')
            }
        })
    },
    (req, res) => {
        res.end('Yay! You made it')
    }
)

createServer(auth).listen(8000)

Keywords

FAQs

Package last updated on 29 Sep 2017

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc