Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 "use strict"; | 5 "use strict"; |
| 6 | 6 |
| 7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
| 8 // in runtime.js: | 8 // in runtime.js: |
| 9 // var $Object = global.Object | 9 // var $Object = global.Object |
| 10 // var $WeakMap = global.WeakMap | 10 // var $WeakMap = global.WeakMap |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 ); | 294 ); |
| 295 } | 295 } |
| 296 | 296 |
| 297 // Combinators. | 297 // Combinators. |
| 298 | 298 |
| 299 function PromiseCast(x) { | 299 function PromiseCast(x) { |
| 300 // TODO(rossberg): cannot do better until we support @@create. | 300 // TODO(rossberg): cannot do better until we support @@create. |
| 301 return IsPromise(x) ? x : new this(function(resolve) { resolve(x) }); | 301 return IsPromise(x) ? x : new this(function(resolve) { resolve(x) }); |
| 302 } | 302 } |
| 303 | 303 |
| 304 function PromiseAll(values) { | 304 function PromiseAll(iterable) { |
| 305 var deferred = %_CallFunction(this, PromiseDeferred); | 305 var deferred = %_CallFunction(this, PromiseDeferred); |
| 306 var resolutions = []; | 306 var resolutions = []; |
| 307 if (!%_IsArray(values)) { | |
| 308 deferred.reject(MakeTypeError('invalid_argument')); | |
| 309 return deferred.promise; | |
| 310 } | |
| 311 try { | 307 try { |
| 312 var count = values.length; | 308 var count = 0; |
| 309 var i = 0; | |
| 310 for (var value of iterable) { | |
| 311 this.resolve(value).then( | |
| 312 // Nested scope to get closure over current i (and avoid .bind). | |
|
arv (Not doing code reviews)
2015/02/23 16:40:39
Andreas, do you think it would make sense to switc
rossberg
2015/02/23 17:38:40
Probably. The comment is outdated either way, beca
arv (Not doing code reviews)
2015/02/23 17:42:45
Yup.
I'll make the change... scratch that. Builti
| |
| 313 // TODO(rossberg): Use for-let instead once available. | |
| 314 (function(i) { | |
| 315 return function(x) { | |
| 316 resolutions[i] = x; | |
| 317 if (--count === 0) deferred.resolve(resolutions); | |
| 318 } | |
| 319 })(i), | |
| 320 function(r) { deferred.reject(r); }); | |
| 321 ++i; | |
| 322 ++count; | |
| 323 } | |
| 324 | |
| 313 if (count === 0) { | 325 if (count === 0) { |
| 314 deferred.resolve(resolutions); | 326 deferred.resolve(resolutions); |
| 315 } else { | |
| 316 for (var i = 0; i < values.length; ++i) { | |
| 317 this.resolve(values[i]).then( | |
| 318 (function() { | |
| 319 // Nested scope to get closure over current i (and avoid .bind). | |
| 320 // TODO(rossberg): Use for-let instead once available. | |
| 321 var i_captured = i; | |
| 322 return function(x) { | |
| 323 resolutions[i_captured] = x; | |
| 324 if (--count === 0) deferred.resolve(resolutions); | |
| 325 }; | |
| 326 })(), | |
| 327 function(r) { deferred.reject(r) } | |
| 328 ); | |
| 329 } | |
| 330 } | 327 } |
| 328 | |
| 331 } catch (e) { | 329 } catch (e) { |
| 332 deferred.reject(e) | 330 deferred.reject(e) |
| 333 } | 331 } |
| 334 return deferred.promise; | 332 return deferred.promise; |
| 335 } | 333 } |
| 336 | 334 |
| 337 function PromiseOne(values) { | 335 function PromiseRace(iterable) { |
| 338 var deferred = %_CallFunction(this, PromiseDeferred); | 336 var deferred = %_CallFunction(this, PromiseDeferred); |
| 339 if (!%_IsArray(values)) { | |
| 340 deferred.reject(MakeTypeError('invalid_argument')); | |
| 341 return deferred.promise; | |
| 342 } | |
| 343 try { | 337 try { |
| 344 for (var i = 0; i < values.length; ++i) { | 338 for (var value of iterable) { |
| 345 this.resolve(values[i]).then( | 339 this.resolve(value).then( |
| 346 function(x) { deferred.resolve(x) }, | 340 function(x) { deferred.resolve(x) }, |
| 347 function(r) { deferred.reject(r) } | 341 function(r) { deferred.reject(r) }); |
| 348 ); | |
| 349 } | 342 } |
| 350 } catch (e) { | 343 } catch (e) { |
| 351 deferred.reject(e) | 344 deferred.reject(e) |
| 352 } | 345 } |
| 353 return deferred.promise; | 346 return deferred.promise; |
| 354 } | 347 } |
| 355 | 348 |
| 356 | 349 |
| 357 // Utility for debugger | 350 // Utility for debugger |
| 358 | 351 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 381 | 374 |
| 382 %CheckIsBootstrapping(); | 375 %CheckIsBootstrapping(); |
| 383 %AddNamedProperty(global, 'Promise', $Promise, DONT_ENUM); | 376 %AddNamedProperty(global, 'Promise', $Promise, DONT_ENUM); |
| 384 %AddNamedProperty( | 377 %AddNamedProperty( |
| 385 $Promise.prototype, symbolToStringTag, "Promise", DONT_ENUM | READ_ONLY); | 378 $Promise.prototype, symbolToStringTag, "Promise", DONT_ENUM | READ_ONLY); |
| 386 InstallFunctions($Promise, DONT_ENUM, [ | 379 InstallFunctions($Promise, DONT_ENUM, [ |
| 387 "defer", PromiseDeferred, | 380 "defer", PromiseDeferred, |
| 388 "accept", PromiseResolved, | 381 "accept", PromiseResolved, |
| 389 "reject", PromiseRejected, | 382 "reject", PromiseRejected, |
| 390 "all", PromiseAll, | 383 "all", PromiseAll, |
| 391 "race", PromiseOne, | 384 "race", PromiseRace, |
| 392 "resolve", PromiseCast | 385 "resolve", PromiseCast |
| 393 ]); | 386 ]); |
| 394 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 387 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 395 "chain", PromiseChain, | 388 "chain", PromiseChain, |
| 396 "then", PromiseThen, | 389 "then", PromiseThen, |
| 397 "catch", PromiseCatch | 390 "catch", PromiseCatch |
| 398 ]); | 391 ]); |
| 399 | 392 |
| 400 })(); | 393 })(); |
| OLD | NEW |