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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 } | 254 } |
255 } | 255 } |
256 return x; | 256 return x; |
257 } | 257 } |
258 | 258 |
259 | 259 |
260 // Combinators. | 260 // Combinators. |
261 | 261 |
262 function PromiseCast(x) { | 262 function PromiseCast(x) { |
263 // TODO(rossberg): cannot do better until we support @@create. | 263 // TODO(rossberg): cannot do better until we support @@create. |
264 return IsPromise(x) ? x : this.resolve(x); | 264 return IsPromise(x) ? x : new this(function(resolve) { resolve(x) }); |
265 } | 265 } |
266 | 266 |
267 function PromiseAll(values) { | 267 function PromiseAll(values) { |
268 var deferred = %_CallFunction(this, PromiseDeferred); | 268 var deferred = %_CallFunction(this, PromiseDeferred); |
269 var resolutions = []; | 269 var resolutions = []; |
270 if (!%_IsArray(values)) { | 270 if (!%_IsArray(values)) { |
271 deferred.reject(MakeTypeError('invalid_argument')); | 271 deferred.reject(MakeTypeError('invalid_argument')); |
272 return deferred.promise; | 272 return deferred.promise; |
273 } | 273 } |
274 try { | 274 try { |
275 var count = values.length; | 275 var count = values.length; |
276 if (count === 0) { | 276 if (count === 0) { |
277 deferred.resolve(resolutions); | 277 deferred.resolve(resolutions); |
278 } else { | 278 } else { |
279 for (var i = 0; i < values.length; ++i) { | 279 for (var i = 0; i < values.length; ++i) { |
280 this.cast(values[i]).then( | 280 this.resolve(values[i]).then( |
281 function(i, x) { | 281 function(i, x) { |
282 resolutions[i] = x; | 282 resolutions[i] = x; |
283 if (--count === 0) deferred.resolve(resolutions); | 283 if (--count === 0) deferred.resolve(resolutions); |
284 }.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available | 284 }.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available |
285 function(r) { deferred.reject(r) } | 285 function(r) { deferred.reject(r) } |
286 ); | 286 ); |
287 } | 287 } |
288 } | 288 } |
289 } catch (e) { | 289 } catch (e) { |
290 deferred.reject(e) | 290 deferred.reject(e) |
291 } | 291 } |
292 return deferred.promise; | 292 return deferred.promise; |
293 } | 293 } |
294 | 294 |
295 function PromiseOne(values) { | 295 function PromiseOne(values) { |
296 var deferred = %_CallFunction(this, PromiseDeferred); | 296 var deferred = %_CallFunction(this, PromiseDeferred); |
297 if (!%_IsArray(values)) { | 297 if (!%_IsArray(values)) { |
298 deferred.reject(MakeTypeError('invalid_argument')); | 298 deferred.reject(MakeTypeError('invalid_argument')); |
299 return deferred.promise; | 299 return deferred.promise; |
300 } | 300 } |
301 try { | 301 try { |
302 for (var i = 0; i < values.length; ++i) { | 302 for (var i = 0; i < values.length; ++i) { |
303 this.cast(values[i]).then( | 303 this.resolve(values[i]).then( |
304 function(x) { deferred.resolve(x) }, | 304 function(x) { deferred.resolve(x) }, |
305 function(r) { deferred.reject(r) } | 305 function(r) { deferred.reject(r) } |
306 ); | 306 ); |
307 } | 307 } |
308 } catch (e) { | 308 } catch (e) { |
309 deferred.reject(e) | 309 deferred.reject(e) |
310 } | 310 } |
311 return deferred.promise; | 311 return deferred.promise; |
312 } | 312 } |
313 | 313 |
314 //------------------------------------------------------------------- | 314 //------------------------------------------------------------------- |
315 | 315 |
316 function SetUpPromise() { | 316 function SetUpPromise() { |
317 %CheckIsBootstrapping() | 317 %CheckIsBootstrapping() |
318 var global_receiver = %GlobalReceiver(global); | 318 var global_receiver = %GlobalReceiver(global); |
319 global_receiver.Promise = $Promise; | 319 global_receiver.Promise = $Promise; |
320 InstallFunctions($Promise, DONT_ENUM, [ | 320 InstallFunctions($Promise, DONT_ENUM, [ |
321 "defer", PromiseDeferred, | 321 "defer", PromiseDeferred, |
322 "resolve", PromiseResolved, | 322 "accept", PromiseResolved, |
323 "reject", PromiseRejected, | 323 "reject", PromiseRejected, |
324 "all", PromiseAll, | 324 "all", PromiseAll, |
325 "race", PromiseOne, | 325 "race", PromiseOne, |
326 "cast", PromiseCast | 326 "resolve", PromiseCast |
327 ]); | 327 ]); |
328 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 328 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
329 "chain", PromiseChain, | 329 "chain", PromiseChain, |
330 "then", PromiseThen, | 330 "then", PromiseThen, |
331 "catch", PromiseCatch | 331 "catch", PromiseCatch |
332 ]); | 332 ]); |
333 } | 333 } |
334 | 334 |
335 SetUpPromise(); | 335 SetUpPromise(); |
OLD | NEW |