0.2.0 • Published 10 years ago

hybridflows v0.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

hybridflows

A hybrid module for asynchronous flow control with error handling and data forwarding. Hybrid means the module can be used on server an client and thus requirejs is used as module loader. Please check jsdoc for detailed module documentation.

serial example

    requirejs([ 'require' ],function (require) {
    
        var hybridflows = require('hybridflows');

        // needed on server to enable setTimeout()
        var timers = require('timers');
        var seconds = 1000
        
        var serialTasks = [
        
            function (data, next) {
        
                setTimeout(function () {
        
                    data = data + 1;
                    next(null, data);
        
                }, 1 * seconds);
        
            }, function (data, next) {
        
                setTimeout(function () {
        
                    data = data + 2;
                    next(null, data);
                    //next(new Error('some error'));
        
                }, 2 * seconds);
        
            }, function (data, next) {
        
                setTimeout(function () {
        
                    data = data + 3;
                    next(null, data);
        
                }, 3 * seconds);
            }
        ];
        
        var serialOptions = {
        
            'tasks': serialTasks,
            'data': 0
        };
        
        hybridflows.serial(serialOptions, function (error, data) {
        
            if (error) {
        
                console.error('Serial: ' + error);
        
            } else {
        
                console.log('Serial: ' + data);
            }
        
        });
        
    });

parallel example

    requirejs([ 'require' ],function (require) {

        var hybridflows = require('hybridflows');
        
        // needed on server to enable setTimeout()
        var timers = require('timers');
        var seconds = 1000;
        
        var parallelTasks = [
        
            function (data, done) {
        
                setTimeout(function () {
        
                    data.push(1);
                    done();
        
                }, 1 * seconds);
        
            }, function (data, done) {
        
                setTimeout(function () {
        
                    data.push(2);
                    done();
                    //done(new Error('some error'));
        
                }, 2 * seconds);
        
            }, function (data, done) {
        
                setTimeout(function () {
        
                    data.push(3);
                    done();
        
                }, 3 * seconds);
            }
        ];
        
        var parallelOptions = {
        
            'tasks': parallelTasks,
            'data': []
        };
        
        hybridflows.parallel(parallelOptions, function (errors, data) {
        
            if (Array.isArray(errors)) {
        
                errors.forEach(function (error) {
        
                    console.error('Parallel: ' + error);
        
                });
        
            } else {
        
                var result = 0;
                data.forEach(function (number) {
        
                    result += number;
                });
        
                console.log('Parallel: ' + result);
        
            }
        });
        
    });
0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago