Permalink
Browse files

when.any handles object properties now, and is probably slightly fast…

…er, but larger
  • Loading branch information...
1 parent ecf85d5 commit 17edb8249f99ef34e1608795ceade8591b146fce @briancavalier briancavalier committed Jun 27, 2012
Showing with 42 additions and 17 deletions.
  1. +42 −17 when.js
View
@@ -406,12 +406,10 @@ define(function() { "use strict";
if(promisesOrValues instanceof Array) {
results = [];
toResolve = Math.max(0, Math.min(howMany, promisesOrValues.length >>> 0));
- forEach = forEachElement;
} else {
results = {};
toResolve = 0;
- forEachKey(promisesOrValues, function() { toResolve++; });
- forEach = forEachKey;
+ forEach(promisesOrValues, function() { toResolve++; });
}
// Wrapper so that rejecter can be replaced
@@ -509,11 +507,42 @@ define(function() { "use strict";
*/
function any(promisesOrValues, callback, errback, progressHandler) {
- function unwrapSingleResult(val) {
- return callback ? callback(val[0]) : val[0];
- }
+ checkCallbacks(2, arguments);
+
+ return when(promisesOrValues, function(promisesOrValues) {
+
+ var deferred, resolver, rejecter, handleProgress;
+
+ deferred = defer();
+
+ function resolve(val) {
+ resolver(val);
+ }
+
+ // Wrapper so that rejecter can be replaced
+ function reject(err) {
+ rejecter(err);
+ }
- return some(promisesOrValues, 1, unwrapSingleResult, errback, progressHandler);
+ // Wrapper so that progress can be replaced
+ function progress(update) {
+ handleProgress(update);
+ }
+
+ deferred.promise.always(function () {
+ resolver = rejecter = handleProgress = noop;
+ });
+
+ resolver = deferred.resolve;
+ rejecter = deferred.reject;
+ handleProgress = deferred.progress;
+
+ forEach(promisesOrValues, function (p) {
+ when(p, resolve, reject, progress);
+ });
+
+ return when(deferred, callback, errback, progressHandler);
+ });
}
/**
@@ -546,19 +575,11 @@ define(function() { "use strict";
*/
function _map(promisesOrValues, mapFunc) {
- var results, iterate;
-
- if(promisesOrValues instanceof Array) {
- results = [];
- iterate = forEachElement;
- } else {
- results = {};
- iterate = forEachKey;
- }
+ var results = (promisesOrValues instanceof Array) ? [] : {};
// Since mapFunc may be async, get all invocations of it into flight
// asap, and then use reduce() to collect all the results
- iterate(promisesOrValues, function(val, i) {
+ forEach(promisesOrValues, function (val, i) {
results[i] = when(val, mapFunc);
});
@@ -694,6 +715,10 @@ define(function() { "use strict";
}
}
+ function forEach(objectOrArray, lambda) {
+ (objectOrArray instanceof Array ? forEachElement : forEachKey)(objectOrArray, lambda);
+ }
+
// ES5 reduce implementation if native not available
// See: http://es5.github.com/#x15.4.4.21 as there are many
// specifics and edge cases.

0 comments on commit 17edb82

Please sign in to comment.