npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-fnstack

v1.1.5

Published

Async middleware function stack inspired by expressjs

Downloads

9

Readme

npm downloads Open issues Pull requests

node-fnstack

Async middleware function stack inspired by ExpressJS

Why ?

This is a standalone, simple and async version of middleware function stack popularized by ExpressJS. It can be used for setting up arbitrary function stacks.

In itself, it is a powerful concept of building a one way processing pipeline composed of functions, all working sequentially on the same data.

Quick Tour

Install:

npm install node-fnstack --save

A typical stack:

// import
const FnStack = require('node-fnstack');

// create one
const stack = new FnStack();

// setup async middleware
stack.use( cors );
stack.use( bodyParser );
stack.use( dbConnect );
stack.use( session );

// your custom async function
stack.use( fn );

// error handlers as well
stack.catch( errorHandler );

Calling the stack:

// the entire stack as a single async function
const fn = stack.fn;

// call as an async function, 
// arguments will be offered to each middleware
// any error will be offered to the next catch middleware
await fn(arg1, arg2);

Going meta level:

// since the entire stack is also a single async function
const fn = stack.fn;

// it can be used as an async middleware too
const parentStack = new FnStack();
parentStack.use(fn);

// and it will be called from parent
await parentStack.fn(arg1, arg2);

API

A single class FnStack is exported.

FnStack

Represents the async middleware stack.

Methods

use(...fns)

Add one or more async middleware function to the stack. When stack is invoked, these functions are invoked in the same order they were added, and with same arguments as the stack is invoked with.

Example:

const FnStack = require('node-fnstack');

const stack = new FnStack();

stack.use(  
           async (x) => console.log('fn1',x), 
           async (x) => console.log('fn2',x)
         );
stack.use(async (x) => console.log('fn3',x));

const run = async () => await stack.fn(10);

run();

Output:

fn1 10
fn2 10
fn3 10

catch(...fns)

Add one or more async error handler function to the stack. When an error is thrown by any stack middleware, the normal middleware are skipped and instead the next error handler function in stack is called with error. Error handler functions are NOT called if there is no error propagating along the stack.

Example:

const FnStack = require('node-fnstack');

const stack = new FnStack();

stack.use(async (x) => {throw new Error('crash');});
stack.use(async (x) => console.log('fn1',x));
stack.catch(async (e) => console.log('caught'));
stack.use(async (x) => console.log('fn2',x));

const run = async () => await stack.fn(10);

run();

Output:

caught
fn2 10

Properties

fn

This property represents the entire stack as a single async middleware function.

Example:

const FnStack = require('node-fnstack');

const stack = new FnStack();
stack.use(async () => console.log('fn11'));
stack.use(async () => console.log('fn12'));

const parentStack = new FnStack();
parentStack.use(async () => console.log('fn1'));

parentStack.use(stack.fn); // <-- child stack

parentStack.use(async () => console.log('fn2'));

const run = async () => await parentStack.fn(10);

run();

Output:

fn1
fn11
fn12
fn2

NO_NEXT

Sometimes its required to abort the stack execution. That is, no further middleware functions should be invoked. For this a middleware function can return or resolve a special value: FnStack.NO_NEXT

Example:

const FnStack = require('node-fnstack');

const stack = new FnStack();

stack.use(async (x) => console.log('fn1',x));
stack.use(async (x) => FnStack.NO_NEXT);
stack.use(async (x) => console.log('fn2',x));

const run = async () => await stack.fn(10);

run();

Output:

fn1 10