diff options
| author | Alexander Shtuchkin | 2013-02-23 18:54:55 +0400 | 
|---|---|---|
| committer | Igor Minar | 2013-02-25 10:58:05 -0800 | 
| commit | e27bb6eb132a68665c8fca3f5a216b19b1129ba6 (patch) | |
| tree | 1e1d4d8943d15089a8d8d01549c2cca02f9f9479 /src/ng/q.js | |
| parent | 7b236b29aa3a6f6dfe722815e0a2667d9b7f0899 (diff) | |
| download | angular.js-e27bb6eb132a68665c8fca3f5a216b19b1129ba6.tar.bz2 | |
feat($q): $q.all() now accepts hash
When waiting for several promises at once, it is often desirable to
have them by name, not just by index in array.
Example of this kind of interface already implemented would be a
$routeProvider.when(url, {resolve: <hash of promises>}), where
resources/promises are given by names, and then results accessed
by names in controller.
Diffstat (limited to 'src/ng/q.js')
| -rw-r--r-- | src/ng/q.js | 35 | 
1 files changed, 18 insertions, 17 deletions
| diff --git a/src/ng/q.js b/src/ng/q.js index f3bd1d6e..6cb92102 100644 --- a/src/ng/q.js +++ b/src/ng/q.js @@ -377,29 +377,30 @@ function qFactory(nextTick, exceptionHandler) {     * Combines multiple promises into a single promise that is resolved when all of the input     * promises are resolved.     * -   * @param {Array.<Promise>} promises An array of promises. -   * @returns {Promise} Returns a single promise that will be resolved with an array of values, -   *   each value corresponding to the promise at the same index in the `promises` array. If any of +   * @param {Array.<Promise>|Object.<Promise>} promises An array or hash of promises. +   * @returns {Promise} Returns a single promise that will be resolved with an array/hash of values, +   *   each value corresponding to the promise at the same index/key in the `promises` array/hash. If any of     *   the promises is resolved with a rejection, this resulting promise will be resolved with the     *   same rejection.     */    function all(promises) {      var deferred = defer(), -        counter = promises.length, -        results = []; - -    if (counter) { -      forEach(promises, function(promise, index) { -        ref(promise).then(function(value) { -          if (index in results) return; -          results[index] = value; -          if (!(--counter)) deferred.resolve(results); -        }, function(reason) { -          if (index in results) return; -          deferred.reject(reason); -        }); +        counter = 0, +        results = isArray(promises) ? [] : {}; + +    forEach(promises, function(promise, key) { +      counter++; +      ref(promise).then(function(value) { +        if (results.hasOwnProperty(key)) return; +        results[key] = value; +        if (!(--counter)) deferred.resolve(results); +      }, function(reason) { +        if (results.hasOwnProperty(key)) return; +        deferred.reject(reason);        }); -    } else { +    }); + +    if (counter === 0) {        deferred.resolve(results);      } | 
