function waitForAll(promises, failOnReject, timeout) {
        var masterDeferred = new $.Deferred(),
            results = [],
            count = 0,
            sawRejects = false;

        if (!promises || promises.length === 0) {
            masterDeferred.resolve();
            return masterDeferred.promise();
        }

        // set defaults if needed
        failOnReject = (failOnReject === undefined) ? false : true;

        if (timeout !== undefined) {
            withTimeout(masterDeferred, timeout);
        }

        promises.forEach(function (promise) {
            promise
                .fail(function (err) {
                    sawRejects = true;
                })
                .done(function (result) {
                    results.push(result);
                })
                .always(function () {
                    count++;
                    if (count === promises.length) {
                        if (failOnReject && sawRejects) {
                            masterDeferred.reject();
                        } else {
                            masterDeferred.resolve(results);
                        }
                    }
                });
        });

        return masterDeferred.promise();
    }
Allows waiting for all the promises to be either resolved or rejected.
Unlike $.when(), it does not call .fail() or .always() handlers on first
reject. The caller should take all the precaution to make sure all the
promises passed to this function are completed to avoid blocking.

If failOnReject is set to true, promise returned by the function will be
rejected if at least one of the promises was rejected. The default value
is false, which will cause the call to this function to be always
successfully resolved.

If timeout is specified, the promise will be rejected on timeout as per
Async.withTimeout.

@param {!Array.<$.Promise>} promises Array of promises to wait for
@param {boolean=} failOnReject       Whether to reject or not if one of the promises has been rejected.
@param {number=} timeout             Number of milliseconds to wait until rejecting the promise

@return {$.Promise} A Promise which will be resolved once all dependent promises are resolved.
                    It is resolved with an array of results from the successfully resolved dependent promises.
                    The resulting array may not be in the same order or contain as many items as there were
                    promises to wait on and it will contain 'undefined' entries for those promises that resolve
                    without a result.