Skip to content

Instantly share code, notes, and snippets.

@austo
Created November 27, 2013 04:47
Show Gist options
  • Select an option

  • Save austo/7670799 to your computer and use it in GitHub Desktop.

Select an option

Save austo/7670799 to your computer and use it in GitHub Desktop.
execute array of functions serially
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