Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. // FullParallel businessLogichronous engine
  2. function fullParallel(callbacks, last) {
  3. var results = [];
  4. var result_count = 0;
  5.  
  6. callbacks.forEach(function(callback, index) {
  7.  
  8. callback( function() {
  9. results[index] = Array.prototype.slice.call(arguments);
  10. result_count++;
  11.  
  12. if(result_count == callbacks.length) { // the termination guard is checked in the businessLogic!!
  13. last(results);
  14. }
  15. });
  16.  
  17. });
  18. }
  19.  
  20. // Example task (business logic). Despite the name, this is a normal function
  21. function businessLogic(arg, callback) {
  22. var delay = Math.floor(Math.random() * 5 + 1) * 100; // random ms
  23. console.log('businessLogic with \''+arg+'\', return in '+delay+' ms');
  24. setTimeout(function() { callback(arg * 2); }, delay);
  25. }
  26.  
  27. function end(results) { console.log('Done', results); }
  28.  
  29. var theCallbacks = [
  30. function(next) {
  31. businessLogic(1, next);
  32. },
  33. function(next) { businessLogic(2, next); },
  34. function(next) { businessLogic(3, next); },
  35. function(next) { businessLogic(4, next); },
  36. function(next) { businessLogic(5, next); },
  37. function(next) { businessLogic(6, next); }
  38. ];
  39.  
  40. fullParallel(theCallbacks, end);
Add Comment
Please, Sign In to add comment