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.

Revisions

  1. austo created this gist Nov 27, 2013.
    42 changes: 42 additions & 0 deletions serial.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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);