Small library to manage async functions and avoid "callback hell"
Well... Yes. But this one is dead simple. Simple library - less overhead and less bugs.
npm install flow-manager
Create "Flow" object and add some steps. "Step" is a simple callback with two arguments: flow object and data object.
var Flows = require('flow-manager');
Flows
.create();
.addStep(function (flow, data) {
// some awesome stuff
data.step1 = 'done';
flow.next(data);
})
.addStep(function (flow, data) {
console.log(data); // {initialData: {isAwesome: true}, step1: true}
// always destroy flow object at the end:
Flows.destroy(flow);
// OR if this is the last step, after .next() it will be destroyed automatically
// flow.next();
//
})
.execute({
initialData: {isAwesome: true}
});
var Flows = require('flow-manager');
Flows
.create();
.addStep(function (flow, data) {
data.step1 = true;
throw new Error('Oh!');
flow.next(data);
})
.addStep(function (flow, data) {
data.step2 = true;
flow.next(data);
})
.addStep(function (flow, data) {
console.log('All OK', data); // Expected: All OK {step1: true, step2: true}
Flows.destroy(flow);
})
.catchError(function (data) {
console.log('Error', data);
})
.execute({
step1: false,
step2: false
});
// Result:
// Error {step1: true, step2: false}
- flow.next(object) - Goes to the next step. If object is provided - next step will receive it as a data
- flow.nextFrom(int, object) - Same as .next just another step will be used, int - step number
- flow.getStep() - receive current step number
- flow.repeat(object) - repeat current step
- flow.execute(object) - start flow, object holds data for first step, if object is not provided - then first step will receive null
Real world is much more complex, than examples, take a look: test.js