OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // Combinators. | 241 // Combinators. |
242 | 242 |
243 function PromiseCast(x) { | 243 function PromiseCast(x) { |
244 // TODO(rossberg): cannot do better until we support @@create. | 244 // TODO(rossberg): cannot do better until we support @@create. |
245 return IsPromise(x) ? x : this.resolve(x); | 245 return IsPromise(x) ? x : this.resolve(x); |
246 } | 246 } |
247 | 247 |
248 function PromiseAll(values) { | 248 function PromiseAll(values) { |
249 var deferred = %_CallFunction(this, PromiseDeferred); | 249 var deferred = %_CallFunction(this, PromiseDeferred); |
250 var resolutions = []; | 250 var resolutions = []; |
| 251 if (!%_IsArray(values)) { |
| 252 deferred.reject(MakeTypeError('invalid_argument')); |
| 253 return deferred.promise; |
| 254 } |
251 try { | 255 try { |
252 var count = values.length; | 256 var count = values.length; |
253 if (count === 0) { | 257 if (count === 0) { |
254 deferred.resolve(resolutions); | 258 deferred.resolve(resolutions); |
255 } else { | 259 } else { |
256 for (var i = 0; i < values.length; ++i) { | 260 for (var i = 0; i < values.length; ++i) { |
257 this.cast(values[i]).chain( | 261 this.cast(values[i]).chain( |
258 function(i, x) { | 262 function(i, x) { |
259 resolutions[i] = x; | 263 resolutions[i] = x; |
260 if (--count === 0) deferred.resolve(resolutions); | 264 if (--count === 0) deferred.resolve(resolutions); |
261 }.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available | 265 }.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available |
262 function(r) { deferred.reject(r) } | 266 function(r) { deferred.reject(r) } |
263 ); | 267 ); |
264 } | 268 } |
265 } | 269 } |
266 } catch (e) { | 270 } catch (e) { |
267 deferred.reject(e) | 271 deferred.reject(e) |
268 } | 272 } |
269 return deferred.promise; | 273 return deferred.promise; |
270 } | 274 } |
271 | 275 |
272 function PromiseOne(values) { | 276 function PromiseOne(values) { |
273 var deferred = %_CallFunction(this, PromiseDeferred); | 277 var deferred = %_CallFunction(this, PromiseDeferred); |
| 278 if (!%_IsArray(values)) { |
| 279 deferred.reject(MakeTypeError('invalid_argument')); |
| 280 return deferred.promise; |
| 281 } |
274 try { | 282 try { |
275 for (var i = 0; i < values.length; ++i) { | 283 for (var i = 0; i < values.length; ++i) { |
276 this.cast(values[i]).chain( | 284 this.cast(values[i]).chain( |
277 function(x) { deferred.resolve(x) }, | 285 function(x) { deferred.resolve(x) }, |
278 function(r) { deferred.reject(r) } | 286 function(r) { deferred.reject(r) } |
279 ); | 287 ); |
280 } | 288 } |
281 } catch (e) { | 289 } catch (e) { |
282 deferred.reject(e) | 290 deferred.reject(e) |
283 } | 291 } |
(...skipping 15 matching lines...) Expand all Loading... |
299 "cast", PromiseCast | 307 "cast", PromiseCast |
300 ]); | 308 ]); |
301 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 309 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
302 "chain", PromiseChain, | 310 "chain", PromiseChain, |
303 "then", PromiseThen, | 311 "then", PromiseThen, |
304 "catch", PromiseCatch | 312 "catch", PromiseCatch |
305 ]); | 313 ]); |
306 } | 314 } |
307 | 315 |
308 SetUpPromise(); | 316 SetUpPromise(); |
OLD | NEW |