Skip to content

Simple middleware pattern implementation on top of async.series

License

Notifications You must be signed in to change notification settings

elmigranto/jsmw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsmw

npm install jsmw for a simple middleware pattern implementation on top of async.series.

// Require a function.
const jsmw = require('jsmw');

// Create the chain of middlewares.
const chain = jsmw()
  .use((ctx, next) => {
    ctx.beforeTick = Date.now();
    next();
  })
  .use((ctx, next) => {
    process.nextTick(() => {
      ctx.afterTick = Date.now();
      next();
    });
  });

// Execute your chain with different contexts.
const context = {};
chain.execute(context, err => {
  console.log(err || context);
  // { beforeTick: 1451920587070, afterTick: 1451920587073 }
});

// Supports nested chains (context passed from outer one).
const nestingContext = {};
const nestedChain = jsmw()
  .use(chain)
  .use((ctx, next) => {
    ctx.diff = ctx.afterTick - ctx.beforeTick;
    next();
  });

nestedChain.execute(nestingContext, err => {
  console.log(err || nestingContext);
  // { beforeTick: 1451920587072, afterTick: 1451920587123, diff: 51 }
});

Tests have more examples.

Compatibility

Works with node >=4 by default, but should run anywhere async does, with transpilation being required for some JS environments.

About

Simple middleware pattern implementation on top of async.series

Resources

License

Stars

Watchers

Forks

Packages