Giter VIP home page Giter VIP logo

get-flow's Introduction

get-flow

Node.js Flow Control Utility, powerful and easy to use

Installation

$ git clone --branch=master git://github.com/enricostara/get-flow.git
$ cd get-flow
$ npm install

Coding

Function runSeries(taskList, callback, ...) runs an Array of tasks - and yes, you can mix synchronous and asynchronous tasks! - in series, calling back when done or immediately if an exception occurs.

  • Each task must be a sync or async Function and all the async tasks must have the first argument named callback in order to be recognised as asynchronous.

  • Synchronous task will be internally executed surrounded by a try/catch statement, any exception stops the flow and it will be passed to the main callback as first argument.

  • ASynchronous task must manage any exception internally and propagate it to the callback as first argument

  • The arguments callback and tasks are mandatory (callback can be null but in place anyway), the subsequent arguments (if any) will be passed only to the first task .

See the following example where [sync, async, sync] tasks are executed in series:

var flow = require('get-flow');

function multiply(x, y) {
    return x * y;
}

function addMillis(callback, x) {  // for ASYNC task the first argument must be the callback and be named 'callback'
    var start = new Date().getTime();
    setTimeout(function () {
        var millis = new Date().getTime() - start;
        callback(null, x + millis);
    }, 100);
}

function sqrt(x) {
    return Math.sqrt(x);
}

flow.runSeries(
    [   // tasks series
        multiply, // sync
        addMillis, // async
        sqrt  // sync again
    ],
    function(ex, result) {    // a proper callback when the last task (sqrt) is done
        console.log('no exception, (ex == null) = %s ', ex == null);
        console.log('result = %s', result);
        var assert = require('assert');
        assert.equal(result >= 10, true);
    },
    2, 3 // x and y passed at first task (multiply)
);

Function retryUntilIsDone(callback, retriesLimit, task, ...) calls the given async task until succeeds or the retries limit has been reached (the default is 10 attempts)

All the arguments are mandatory (callback and retriesLimit can be null but in place anyway), the subsequent arguments (if any) will be passed to the task.

See the following example

var flow = require('get-flow');

var i = 0;

function task(callback, input) {
    setTimeout(function () {
        i++;
        if (i === 5) {
            callback(null, input + i);
        } else {
            callback('err')
        }
    }, 10);
}

flow.retryUntilIsDone(
    function (ex, result) { // a proper callback
        console.log('no exception, (ex == null) = %s ', ex == null);
        console.log('result = %s', result);
        var assert = require('assert');
        assert.equal(result, 15);
    },
    null, // retries limit will be the default = 10
    task, // the async task
    10  // argument for the task
);

Unit Testing

$ npm test

Dependencies

  • get-log: a Node.js Logging Utility, easy to use and ready for production.

License

The project is released under the MIT license

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.