exec-stack
v1.1.0
Published
```javascript $ npm install exec-stack ```
Downloads
2
Readme
ExecStack
Installation
$ npm install exec-stack
Example
const ExecStack = require('exec-stack');
const stack = new ExecStack();
stack.use('cars', next => {
console.log('I\'m specifically interested in cars');
next();
});
stack.use((event, next) => {
console.log('I\'m generic. Hence interested in anything, also', event);
next();
});
stack.run('cars');
stack.run('bikes').then(console.log('Done!'));
Output:
I'm specifically interested in cars
I'm generic. Hence interested in anything, also cars
I'm generic. Hence interested in anything, also bikes
Done!
Methods
Optional arguments are written in cursive.
.use(event, callback)
Push a middleware to the stack. If event is omitted, the callback will be called for every execution of the stack.
Returns the position of the middleware in the stack.
.unuse(position)
Removes a specified middleware from the stack. The middleware is being referenced by a number representing its position in the stack.
.run(event, ...args)
Any other argument that is given to .execute()
will also be passed to the functions in the stack.
Returns a promise that is being resolved after everything has finished.
If any middleware calls next.throw() the promise will be rejected.
.context(context)
Set the context to be applied to middleware. When context is omitted it will be cleared to an empty object.
Breaking the loop
If you want to stop the execution of the stack use next.throw(*error*)
which also will reject the promise returned by .run()