function race(promises) {
    var deferred = defer();

    forEach(promises, function(promise) {
      when(promise).then(deferred.resolve, deferred.reject);
    });

    return deferred.promise;
  }
@ngdoc method
@name $q#race
@kind function

@description
Returns a promise that resolves or rejects as soon as one of those promises
resolves or rejects, with the value or reason from that promise.

@param {Array.<Promise>|Object.<Promise>} promises An array or hash of promises.
@returns {Promise} a promise that resolves or rejects as soon as one of the `promises`
resolves or rejects, with the value or reason from that promise.