Commit 8c11dcc8 authored by Stefan Penner's avatar Stefan Penner

refactor RSVP.all to just use a promise directly

parent 55398c39
...@@ -32,45 +32,43 @@ var define, requireModule; ...@@ -32,45 +32,43 @@ var define, requireModule;
})(); })();
define("rsvp/all", define("rsvp/all",
["rsvp/defer","exports"], ["rsvp/promise","exports"],
function(__dependency1__, __exports__) { function(__dependency1__, __exports__) {
"use strict"; "use strict";
var defer = __dependency1__.defer; var Promise = __dependency1__.Promise;
function all(promises) { function all(promises) {
var results = [], deferred = defer(), remaining = promises.length; return new Promise(function(resolve, reject) {
var results = [], remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
deferred.resolve([]); resolve([]);
} }
var resolver = function(index) { function resolver(index) {
return function(value) { return function(value) {
resolveAll(index, value); resolveAll(index, value);
}; };
}; }
var resolveAll = function(index, value) { function resolveAll(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
deferred.resolve(results); resolve(results);
}
} }
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), reject);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return deferred.promise; });
} }
__exports__.all = all; __exports__.all = all;
}); });
......
This diff is collapsed.
define( define(
["rsvp/defer","exports"], ["rsvp/promise","exports"],
function(__dependency1__, __exports__) { function(__dependency1__, __exports__) {
"use strict"; "use strict";
var defer = __dependency1__.defer; var Promise = __dependency1__.Promise;
function all(promises) { function all(promises) {
var results = [], deferred = defer(), remaining = promises.length; return new Promise(function(resolve, reject) {
var results = [], remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
deferred.resolve([]); resolve([]);
} }
var resolver = function(index) { function resolver(index) {
return function(value) { return function(value) {
resolveAll(index, value); resolveAll(index, value);
}; };
}; }
var resolveAll = function(index, value) { function resolveAll(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
deferred.resolve(results); resolve(results);
}
} }
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), reject);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return deferred.promise; });
} }
__exports__.all = all; __exports__.all = all;
}); });
import { defer } from "rsvp/defer"; import { Promise } from "rsvp/promise";
function all(promises) { function all(promises) {
var results = [], deferred = defer(), remaining = promises.length; return new Promise(function(resolve, reject) {
var results = [], remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
deferred.resolve([]); resolve([]);
} }
var resolver = function(index) { function resolver(index) {
return function(value) { return function(value) {
resolveAll(index, value); resolveAll(index, value);
}; };
}; }
var resolveAll = function(index, value) { function resolveAll(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
deferred.resolve(results); resolve(results);
}
} }
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), reject);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return deferred.promise; });
} }
export { all }; export { all };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment