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();
});
})
.use((ctx, next) => {
ctx.diff = ctx.afterTick - ctx.beforeTick;
next();
});
// Execute your chain with different contexts.
const context = {};
chain.execute(context, err => {
return err
? console.error(err)
: console.log(JSON.stringify(context, null, 2));
});
Tests have more examples.
Works with node >=4
by default, but should run everywhere async works, with transpilation being required for some JS environments.