Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(727)

Unified Diff: src/promise.js

Issue 948843004: Promise.all and race should work with iterables (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Update commment and todo Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/mjsunit/es6/promises.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/promise.js
diff --git a/src/promise.js b/src/promise.js
index c096296b0eb2b48696dc2490e70c2f7243cad653..6d752d60ec88004bcfdca5df2baeb3b3ba6add3e 100644
--- a/src/promise.js
+++ b/src/promise.js
@@ -301,51 +301,44 @@ var lastMicrotaskId = 0;
return IsPromise(x) ? x : new this(function(resolve) { resolve(x) });
}
- function PromiseAll(values) {
+ function PromiseAll(iterable) {
var deferred = %_CallFunction(this, PromiseDeferred);
var resolutions = [];
- if (!%_IsArray(values)) {
- deferred.reject(MakeTypeError('invalid_argument'));
- return deferred.promise;
- }
try {
- var count = values.length;
- if (count === 0) {
- deferred.resolve(resolutions);
- } else {
- for (var i = 0; i < values.length; ++i) {
- this.resolve(values[i]).then(
- (function() {
- // Nested scope to get closure over current i (and avoid .bind).
- // TODO(rossberg): Use for-let instead once available.
- var i_captured = i;
+ var count = 0;
+ var i = 0;
+ for (var value of iterable) {
+ this.resolve(value).then(
+ // Nested scope to get closure over current i.
+ // TODO(arv): Use an inner let binding once available.
+ (function(i) {
return function(x) {
- resolutions[i_captured] = x;
+ resolutions[i] = x;
if (--count === 0) deferred.resolve(resolutions);
- };
- })(),
- function(r) { deferred.reject(r) }
- );
- }
+ }
+ })(i),
+ function(r) { deferred.reject(r); });
+ ++i;
+ ++count;
+ }
+
+ if (count === 0) {
+ deferred.resolve(resolutions);
}
+
} catch (e) {
deferred.reject(e)
}
return deferred.promise;
}
- function PromiseOne(values) {
+ function PromiseRace(iterable) {
var deferred = %_CallFunction(this, PromiseDeferred);
- if (!%_IsArray(values)) {
- deferred.reject(MakeTypeError('invalid_argument'));
- return deferred.promise;
- }
try {
- for (var i = 0; i < values.length; ++i) {
- this.resolve(values[i]).then(
- function(x) { deferred.resolve(x) },
- function(r) { deferred.reject(r) }
- );
+ for (var value of iterable) {
+ this.resolve(value).then(
+ function(x) { deferred.resolve(x) },
+ function(r) { deferred.reject(r) });
}
} catch (e) {
deferred.reject(e)
@@ -388,7 +381,7 @@ var lastMicrotaskId = 0;
"accept", PromiseResolved,
"reject", PromiseRejected,
"all", PromiseAll,
- "race", PromiseOne,
+ "race", PromiseRace,
"resolve", PromiseCast
]);
InstallFunctions($Promise.prototype, DONT_ENUM, [
« no previous file with comments | « no previous file | test/mjsunit/es6/promises.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698