Created
March 11, 2010 20:19
-
-
Save willconant/329603 to your computer and use it in GitHub Desktop.
Flow-JS Serial Sum Example 1
This file contains hidden or 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
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