Skip to content

Instantly share code, notes, and snippets.

@Jxck
Forked from yssk22/paralell.js
Created January 30, 2011 07:13
Show Gist options
  • Save Jxck/802642 to your computer and use it in GitHub Desktop.
Save Jxck/802642 to your computer and use it in GitHub Desktop.

Revisions

  1. @yssk22 yssk22 created this gist Dec 14, 2010.
    38 changes: 38 additions & 0 deletions paralell.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    /***
    * @function make the parallel request filters and finish with the last filter.
    *
    * @example
    *
    * misc.parallel(
    * func1, func2, func3, func4
    * );
    *
    * func1 ---+
    * |
    * func2 ---+---> func4
    * |
    * func3 ---+
    *
    * all the filter functions takes 3 arguments (request, response, next)
    * as usual as Express way.
    */
    exports.parallel = function(){
    var list = arguments;
    return function(req, res, next){
    var current = 0;
    var len = list.length;
    var last = list[len-1];
    function pass(i){
    return function(){
    current += 1;
    if( current == len - 1 ){
    last(req, res, next);
    }
    };
    }
    for(var i=0; i<len-1;i++){
    var fun = list[i];
    fun(req, res, pass(i));
    }
    };
    }