Also provies a small bag of peanuts and half a can of soda.
Install
npm install stewardess
Usage:
var stewardess =require('stewardess')
functionfirst(next){
console.log('first');
next();
}
functionsecond(next){
console.log('second');
next();
}
functionthird(next){
console.log('third');
next();
}
stewardess(
first,
second,
third
).run();
stewardess has lot's of chainable methods:
stewardess(
function(next){
// this function goes second
next();
}
)
.add(function(next){
// this function goes third
next();
})
.addBefore(function(next){
// this function goes first
next();
}),
.before(function(){
// this is called before each function
})
.after(function(){
// this is after before each function
})
.done(function(){
// this is called when all methods finish
})
.error(function(err){
// if any method throws an error,
// or calls next(err), it ends up here
})
.final(function(){
// this is called after done or error is called
})
.context({'some':'object'})// set this-ness for all callbacks
.run();// this starts it
you can also pass arguments into run(), which will be sent to each method along the way
stewardess(
function(meow,mix,next){
meow ==='meow';
mix.meow==='mix';
mix.meow=mix.meow.toUpperCase();
next();
},
function(meow,mix,next){
meow ==='meow';
mix.meow==='MIX';
next();
}
)
.after(function(meow,mix){
// before, after, and done also get arguments
})
.error(function(err,meow,mix){
// error gets arguments with the error
})
.run('meow',{meow:'mix'});
you can reuse a stewardess instance by calling bind()
Here is an example of using stewardess to provide middleware for an http
server. It gives the length of each piece of middleware and for the
entire request.
All of the builtin stewardess plugins require the first argument to be
an object.
stewardess(
functionfirst(options,next){
setTimeout(next,100);
},
functionsecond(options,next){
for(var i =0; i <10000;++i){
Math.pow(i);
}
next();
}
)
.plugin(stewardess.plugins.timer)// print the milliseconds taken for each method
.plugin(stewardess.plugins.hrTimer)// same as timer, but with nanosecond precision
.plugin(stewardess.plugins.overallTime)// print the time for the entire stack to run
.run({});
Mongoose Queries
Stewardess will recognize mongoose queries, and run them for you.
You just need to make sure the first parameter is an
object, and call comment to tell stewardess which property to store the
results under.
stewardess(
MongoosePersonModel
.find()
.select({ name:1, age:1})
.sort('age')
.comment('people'),// this tells stewardess where to put the results
functionprintPeople(options,next){
options.people.forEach(function(person){
console.log(person.name,person.age);
});
next();
}
)
.run({});
Testing
Run npm test. Requires mocha.
License
stewardess is published under an MIT style license.