async.js
function parallel(promises, limit) {
    var d = Q.defer();
    var total = promises.length;
    var results = new Array(total);
    var firstErr;
    var running = 0;
    var finished = 0;
    var next = 0;

    limit = limit || total;

    function sched() {
        while (next < total && running < limit) {
            DEBUG && debug('*** sched #', next, '***', promises[next].inspect());
            exec(next++);
        }
    }

    function exec(id) {
        running += 1;
        var promise = promises[id];
        DEBUG && debug('>>> running', running);
        DEBUG && debug('*** run #', id, '***', promise.inspect());
        promise.then(function (result) {
            DEBUG && debug('#', id, 'then ***', result);
            results[id] = result; // collect all result
            d.notify(promise.inspect());
        }).catch(function (err) {
            DEBUG && debug('#', id, 'catch ***', err);
            firstErr = firstErr || err; // keep the first error
        }).finally(function () {
            DEBUG && debug('#', id, 'finally ***', promise.inspect());
            if (++finished === total) {
                DEBUG && debug('>>> finished all ***', firstErr, results);
                return firstErr ? d.reject(firstErr) : d.resolve(results);
            }
            DEBUG && debug('>>> finished', finished);
            running -= 1;
            sched();
        });
    }

    sched();
    return d.promise;
}
function parallel(promises, limit) {
    var d = Q.defer();
    var total = promises.length;
    var results = new Array(total);
    var firstErr;
    var running = 0;
    var finished = 0;
    var next = 0;

    limit = limit || total;

    function sched() {
        while (next < total && running < limit) {
            DEBUG && debug('*** sched #', next, '***', promises[next].inspect());
            exec(next++);
        }
    }

    function exec(id) {
        running += 1;
        var promise = promises[id];
        DEBUG && debug('>>> running', running);
        DEBUG && debug('*** run #', id, '***', promise.inspect());
        promise.then(function (result) {
            DEBUG && debug('#', id, 'then ***', result);
            results[id] = result; // collect all result
            d.notify(promise.inspect());
        }).catch(function (err) {
            DEBUG && debug('#', id, 'catch ***', err);
            firstErr = firstErr || err; // keep the first error
        }).finally(function () {
            DEBUG && debug('#', id, 'finally ***', promise.inspect());
            if (++finished === total) {
                DEBUG && debug('>>> finished all ***', firstErr, results);
                return firstErr ? d.reject(firstErr) : d.resolve(results);
            }
            DEBUG && debug('>>> finished', finished);
            running -= 1;
            sched();
        });
    }

    sched();
    return d.promise;
}
function parallel ( promises , limit ) { var d = Q . defer ( ) ; var total = promises . length ; var results = new Array ( total ) ; var firstErr ; var running = 0 ; var finished = 0 ; var next = 0 ; limit = limit || total ; function sched ( ) { while ( next < total && running < limit ) { DEBUG && debug ( '*** sched #' , next , '***' , promises [ next ] . inspect ( ) ) ; exec ( next ++ ) ; } } function exec ( id ) { running += 1 ; var promise = promises [ id ] ; DEBUG && debug ( '>>> running' , running ) ; DEBUG && debug ( '*** run #' , id , '***' , promise . inspect ( ) ) ; promise . then ( function ( result ) { DEBUG && debug ( '#' , id , 'then ***' , result ) ; results [ id ] = result ; // collect all result d . notify ( promise . inspect ( ) ) ; } ) . catch ( function ( err ) { DEBUG && debug ( '#' , id , 'catch ***' , err ) ; firstErr = firstErr || err ; // keep the first error } ) . finally ( function ( ) { DEBUG && debug ( '#' , id , 'finally ***' , promise . inspect ( ) ) ; if ( ++ finished === total ) { DEBUG && debug ( '>>> finished all ***' , firstErr , results ) ; return firstErr ? d . reject ( firstErr ) : d . resolve ( results ) ; } DEBUG && debug ( '>>> finished' , finished ) ; running -= 1 ; sched ( ) ; } ) ; } sched ( ) ; return d . promise ; }
!!debug.enabled; 

@param {Array.<promise>} promises
@param {number} [limit]
@returns {promise}
!!debug . enabled ;