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) {
deferred.resolve([]);
}
var resolver = function(index) { if (remaining === 0) {
return function(value) { resolve([]);
resolveAll(index, value); }
};
};
var resolveAll = function(index, value) { function resolver(index) {
results[index] = value; return function(value) {
if (--remaining === 0) { resolveAll(index, value);
deferred.resolve(results); };
} }
};
var rejectAll = function(error) { function resolveAll(index, value) {
deferred.reject(error); results[index] = value;
}; if (--remaining === 0) {
resolve(results);
}
}
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) {
return function(value) {
resolveAll(index, value);
};
};
var resolveAll = function(index, value) { function resolver(index) {
results[index] = value; return function(value) {
if (--remaining === 0) { resolveAll(index, value);
deferred.resolve(results); };
} }
};
var rejectAll = function(error) { function resolveAll(index, value) {
deferred.reject(error); results[index] = value;
}; if (--remaining === 0) {
resolve(results);
}
}
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) {
return function(value) {
resolveAll(index, value);
};
};
var resolveAll = function(index, value) { function resolver(index) {
results[index] = value; return function(value) {
if (--remaining === 0) { resolveAll(index, value);
deferred.resolve(results); };
} }
};
var rejectAll = function(error) { function resolveAll(index, value) {
deferred.reject(error); results[index] = value;
}; if (--remaining === 0) {
resolve(results);
}
}
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 };
\ No newline at end of file
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