Skip to content

Instantly share code, notes, and snippets.

@willconant
Created March 11, 2010 20:19
Show Gist options
  • Save willconant/329603 to your computer and use it in GitHub Desktop.
Save willconant/329603 to your computer and use it in GitHub Desktop.
Flow-JS Serial Sum Example 1
var sys = require('sys');
var flow = require('./flow')
// this is a simple async function for adding an amount to a running total
var currentSum = 0;
function addToSum(amountToAdd, callback) {
setTimeout(function() {
currentSum += amountToAdd;
callback();
}, 100);
}
// this is the array we'd like to execute addToSum on in serial
var values = [5, 7, 1, 3];
// our flow needs one step per item in the values array
var defs = [];
values.forEach(function(val){
defs.push(function(){
addToSum(val, this);
});
});
// and one more step when we're all done
defs.push(function(){
sys.puts("The sum is finally: " + currentSum);
});
// execute the array of functions as a flow
flow.exec.apply(flow, defs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment