Created
November 27, 2013 04:47
-
-
Save austo/7670799 to your computer and use it in GitHub Desktop.
execute array of functions serially
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
| function first(cb) { | |
| console.log('hello from first...'); | |
| cb(); | |
| } | |
| function second(cb) { | |
| console.log('hello from second...'); | |
| cb(); | |
| } | |
| function third(cb) { | |
| console.log('hello from third...'); | |
| cb(); | |
| } | |
| function done() { | |
| console.log('hello from done'); | |
| } | |
| function series(functions, callback) { | |
| var funcs = functions.slice(0); | |
| function processNext(err) { | |
| if (err) { | |
| return callback(err); | |
| } | |
| var args = Array.prototype.slice.call(arguments), | |
| func = funcs.shift(); | |
| if (func) { | |
| // remove error argument | |
| args.shift(); | |
| } | |
| else { | |
| func = callback; | |
| } | |
| args.push(processNext); | |
| func.apply(this, args); | |
| } | |
| processNext.call(this); | |
| } | |
| series([first, second, third], done); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment