hybridflows
A hybrid module for asynchronous flow control with error handling and data forwarding
serial example
requirejs([ 'require' ],function (require) {
var hybridflows = require('hybridflows');
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');
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);
}
});
});