Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FullParallel businessLogichronous engine
- function fullParallel(callbacks, last) {
- var results = [];
- var result_count = 0;
- callbacks.forEach(function(callback, index) {
- callback( function() {
- results[index] = Array.prototype.slice.call(arguments);
- result_count++;
- if(result_count == callbacks.length) { // the termination guard is checked in the businessLogic!!
- last(results);
- }
- });
- });
- }
- // Example task (business logic). Despite the name, this is a normal function
- function businessLogic(arg, callback) {
- var delay = Math.floor(Math.random() * 5 + 1) * 100; // random ms
- console.log('businessLogic with \''+arg+'\', return in '+delay+' ms');
- setTimeout(function() { callback(arg * 2); }, delay);
- }
- function end(results) { console.log('Done', results); }
- var theCallbacks = [
- function(next) {
- businessLogic(1, next);
- },
- function(next) { businessLogic(2, next); },
- function(next) { businessLogic(3, next); },
- function(next) { businessLogic(4, next); },
- function(next) { businessLogic(5, next); },
- function(next) { businessLogic(6, next); }
- ];
- fullParallel(theCallbacks, end);
Add Comment
Please, Sign In to add comment