| 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 } else { | 110 } else { |
| 111 var result = {}; | 111 var result = {}; |
| 112 result.promise = new this(function(resolve, reject) { | 112 result.promise = new this(function(resolve, reject) { |
| 113 result.resolve = resolve; | 113 result.resolve = resolve; |
| 114 result.reject = reject; | 114 result.reject = reject; |
| 115 }) | 115 }) |
| 116 return result; | 116 return result; |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 function PromiseResolved(x) { | 120 function PromiseResolveFunction(x) { |
| 121 if (this === $Promise) { | 121 if (this === $Promise) { |
| 122 // Optimized case, avoid extra closure. | 122 // Optimized case, avoid extra closure. |
| 123 return PromiseSet(new Promise(promiseRaw), +1, x); | 123 return PromiseSet(new Promise(promiseRaw), +1, x); |
| 124 } else { | 124 } else { |
| 125 return new this(function(resolve, reject) { resolve(x) }); | 125 return new this(function(resolve, reject) { resolve(x) }); |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 function PromiseRejected(r) { | 129 function PromiseRejectFunction(r) { |
| 130 if (this === $Promise) { | 130 if (this === $Promise) { |
| 131 // Optimized case, avoid extra closure. | 131 // Optimized case, avoid extra closure. |
| 132 return PromiseSet(new Promise(promiseRaw), -1, r); | 132 return PromiseSet(new Promise(promiseRaw), -1, r); |
| 133 } else { | 133 } else { |
| 134 return new this(function(resolve, reject) { reject(r) }); | 134 return new this(function(resolve, reject) { reject(r) }); |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 | 137 |
| 138 | 138 |
| 139 // Simple chaining. | 139 // Simple chaining. |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 } else { | 238 } else { |
| 239 return x; | 239 return x; |
| 240 } | 240 } |
| 241 } | 241 } |
| 242 | 242 |
| 243 | 243 |
| 244 // Combinators. | 244 // Combinators. |
| 245 | 245 |
| 246 function PromiseCast(x) { | 246 function PromiseCast(x) { |
| 247 // TODO(rossberg): cannot do better until we support @@create. | 247 // TODO(rossberg): cannot do better until we support @@create. |
| 248 return IsPromise(x) ? x : this.resolved(x); | 248 return IsPromise(x) ? x : this.resolve(x); |
| 249 } | 249 } |
| 250 | 250 |
| 251 function PromiseAll(values) { | 251 function PromiseAll(values) { |
| 252 var deferred = this.deferred(); | 252 var deferred = this.deferred(); |
| 253 var resolutions = []; | 253 var resolutions = []; |
| 254 var count = values.length; | 254 var count = values.length; |
| 255 if (count === 0) { | 255 if (count === 0) { |
| 256 deferred.resolve(resolutions); | 256 deferred.resolve(resolutions); |
| 257 } else { | 257 } else { |
| 258 for (var i = 0; i < values.length; ++i) { | 258 for (var i = 0; i < values.length; ++i) { |
| 259 this.cast(values[i]).chain( | 259 this.cast(values[i]).chain( |
| 260 function(i, x) { | 260 function(i, x) { |
| 261 resolutions[i] = x; | 261 resolutions[i] = x; |
| 262 if (--count === 0) deferred.resolve(resolutions); | 262 if (--count === 0) deferred.resolve(resolutions); |
| 263 }.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available | 263 }.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available |
| 264 function(r) { | 264 function(r) { |
| 265 if (count > 0) { count = 0; deferred.reject(r) } | 265 if (count > 0) { count = 0; deferred.reject(r) } |
| 266 } | 266 } |
| 267 ); | 267 ); |
| 268 } | 268 } |
| 269 } | 269 } |
| 270 return deferred.promise; | 270 return deferred.promise; |
| 271 } | 271 } |
| 272 | 272 |
| 273 function PromiseOne(values) { // a.k.a. race | 273 function PromiseRace(values) { // a.k.a. one |
| 274 var deferred = this.deferred(); | 274 var deferred = this.deferred(); |
| 275 var done = false; | 275 var done = false; |
| 276 for (var i = 0; i < values.length; ++i) { | 276 for (var i = 0; i < values.length; ++i) { |
| 277 this.cast(values[i]).chain( | 277 this.cast(values[i]).chain( |
| 278 function(x) { if (!done) { done = true; deferred.resolve(x) } }, | 278 function(x) { if (!done) { done = true; deferred.resolve(x) } }, |
| 279 function(r) { if (!done) { done = true; deferred.reject(r) } } | 279 function(r) { if (!done) { done = true; deferred.reject(r) } } |
| 280 ); | 280 ); |
| 281 } | 281 } |
| 282 return deferred.promise; | 282 return deferred.promise; |
| 283 } | 283 } |
| 284 | 284 |
| 285 //------------------------------------------------------------------- | 285 //------------------------------------------------------------------- |
| 286 | 286 |
| 287 function SetUpPromise() { | 287 function SetUpPromise() { |
| 288 %CheckIsBootstrapping() | 288 %CheckIsBootstrapping() |
| 289 global.Promise = $Promise; | 289 global.Promise = $Promise; |
| 290 InstallFunctions($Promise, DONT_ENUM, [ | 290 InstallFunctions($Promise, DONT_ENUM, [ |
| 291 "deferred", PromiseDeferred, | 291 "deferred", PromiseDeferred, |
| 292 "resolved", PromiseResolved, | 292 "resolve", PromiseResolveFunction, |
| 293 "rejected", PromiseRejected, | 293 "reject", PromiseRejectFunction, |
| 294 "all", PromiseAll, | 294 "all", PromiseAll, |
| 295 "one", PromiseOne, | 295 "race", PromiseRace, |
| 296 "cast", PromiseCast | 296 "cast", PromiseCast |
| 297 ]); | 297 ]); |
| 298 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 298 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 299 "chain", PromiseChain, | 299 "chain", PromiseChain, |
| 300 "then", PromiseThen, | 300 "then", PromiseThen, |
| 301 "catch", PromiseCatch | 301 "catch", PromiseCatch |
| 302 ]); | 302 ]); |
| 303 } | 303 } |
| 304 | 304 |
| 305 SetUpPromise(); | 305 SetUpPromise(); |
| OLD | NEW |