0.1.0 • Published 11 years ago

flow-manager v0.1.0

Weekly downloads
24
License
BSD
Repository
github
Last release
11 years ago

Flow-manager for node

Small library to manage async functions and avoid "callback hell"

Another (async|steps|flow|etc) library?

Well... Yes. But this one is dead simple. Simple library - less overhead and less bugs.

Install

npm install flow-manager

How it works

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}
        });

Catch errors

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}

Full control

  • 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 example

Real world is much more complex, than examples, take a look: test.js

0.1.0

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago

0.0.0

11 years ago