Created
April 17, 2012 19:22
-
-
Save Couto/2408420 to your computer and use it in GitHub Desktop.
Walk along an array of asynchronous functions
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
/** | |
* walk | |
* small function to walk along an array of functions | |
* each function walked gets an extra argument | |
* that argument is a function that should be called | |
* to notify the walk function to step to the next function | |
* the arguments given at call time are passed to the next function | |
* | |
* @function | |
* @param {Array} fns Array of functions | |
* @returns undefined | |
*/ | |
function walk(fns) { | |
(function next() { | |
var args = Array.prototype.slice.call(arguments); | |
fns.length && fns.shift().apply(this, args.concat(next)); | |
}()); | |
} | |
// Usage | |
function a(input, output, next) { | |
// do something asincronous | |
// call next when done, and pass values | |
// to the next function in line | |
next(value1, value2) | |
} | |
function b(value1, value2, next) { | |
// do something asincronous | |
// call next when done, and pass values | |
// to the next function in line | |
next(value1, value2) | |
} | |
walk([a,b]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment