Created
October 9, 2013 20:33
-
-
Save telekosmos/6907865 to your computer and use it in GitHub Desktop.
Asynchronous control flow in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function limited(limit, callbacks, last) { | |
var results = []; | |
var running = 1; | |
var task = 0; | |
function next(){ | |
running--; | |
if(task == callbacks.length && running == 0) { | |
last(results); | |
} | |
while(running < limit && callbacks[task]) { | |
var callback = callbacks[task]; | |
(function(index) { | |
callback(function() { | |
results[index] = Array.prototype.slice.call(arguments); | |
next(); | |
}); | |
})(task); | |
task++; | |
running++; | |
} | |
} | |
next(); | |
} | |
// Example task | |
function async(arg, callback) { | |
var delay = Math.floor(Math.random() * 5 + 1) * 1000; // random ms | |
console.log('async with \''+arg+'\', return in '+delay+' ms'); | |
setTimeout(function() { | |
var result = arg * 2; | |
console.log('Return with \''+arg+'\', result '+result); | |
callback(result); | |
}, delay); | |
} | |
function final(results) { console.log('Done', results); } | |
limited(3, [ | |
function(next) { async(1, next); }, | |
function(next) { async(2, next); }, | |
function(next) { async(3, next); }, | |
function(next) { async(4, next); }, | |
function(next) { async(5, next); }, | |
function(next) { async(6, next); } | |
], final); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Concurrent sequential engine | |
function series(callbacks, last) { | |
var results = []; | |
function another() { | |
var callback = callbacks.shift(); | |
if(callback) { | |
var nextParam = function() { | |
results.push(Array.prototype.slice.call(arguments)); | |
another(); | |
}; | |
callback(nextParam); // callback body: businessLogic(number, theNextFunction) | |
} | |
else { | |
last(results); | |
} | |
} | |
another(); | |
} | |
// Example of (businessLogichronous) task, callback contains the business logic | |
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 final(results) { console.log('Done', results); } | |
var theCallbacks = [ | |
function(next) { businessLogic(1, next); }, // next is a function (first and only parameter) | |
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); } | |
]; | |
series(theCallbacks, final); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment