OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 // Flags: --harmony-promises --harmony-observation --allow-natives-syntax |
| 29 |
| 30 var asyncAssertsExpected = 0; |
| 31 |
| 32 function assertAsyncRan() { ++asyncAssertsExpected } |
| 33 |
| 34 function assertAsync(b, s) { |
| 35 if (b) { |
| 36 print(s, "succeeded") |
| 37 } else { |
| 38 %AbortJS(s + " FAILED!") // Simply throwing here will have no effect. |
| 39 } |
| 40 --asyncAssertsExpected |
| 41 } |
| 42 |
| 43 function assertAsyncDone(iteration) { |
| 44 var iteration = iteration || 0 |
| 45 var dummy = {} |
| 46 Object.observe(dummy, |
| 47 function() { |
| 48 if (asyncAssertsExpected === 0) |
| 49 assertAsync(true, "all") |
| 50 else if (iteration > 10) // Shouldn't take more. |
| 51 assertAsync(false, "all") |
| 52 else |
| 53 assertAsyncDone(iteration + 1) |
| 54 } |
| 55 ) |
| 56 dummy.dummy = dummy |
| 57 } |
| 58 |
| 59 |
| 60 (function() { |
| 61 Promise.resolved(5); |
| 62 Promise.resolved(5).when(undefined, assertUnreachable).when( |
| 63 function(x) { assertAsync(x === undefined, "resolved/when-nohandler") }, |
| 64 assertUnreachable |
| 65 ) |
| 66 assertAsyncRan() |
| 67 })(); |
| 68 |
| 69 (function() { |
| 70 Promise.rejected(5).when(assertUnreachable, undefined).when( |
| 71 function(x) { assertAsync(x === undefined, "rejected/when-nohandler") }, |
| 72 assertUnreachable |
| 73 ) |
| 74 assertAsyncRan() |
| 75 })(); |
| 76 |
| 77 (function() { |
| 78 Promise.resolved(5).then(undefined, assertUnreachable).when( |
| 79 function(x) { assertAsync(x === undefined, "resolved/then-nohandler") }, |
| 80 assertUnreachable |
| 81 ) |
| 82 assertAsyncRan() |
| 83 })(); |
| 84 |
| 85 (function() { |
| 86 Promise.rejected(5).then(assertUnreachable, undefined).when( |
| 87 function(x) { assertAsync(x === undefined, "rejected/then-nohandler") }, |
| 88 assertUnreachable |
| 89 ) |
| 90 assertAsyncRan() |
| 91 })(); |
| 92 |
| 93 (function() { |
| 94 var p1 = Promise.resolved(5) |
| 95 var p2 = Promise.resolved(p1) |
| 96 var p3 = Promise.resolved(p2) |
| 97 p3.when( |
| 98 function(x) { assertAsync(x === p2, "resolved/when") }, |
| 99 assertUnreachable |
| 100 ) |
| 101 assertAsyncRan() |
| 102 })(); |
| 103 |
| 104 (function() { |
| 105 var p1 = Promise.resolved(5) |
| 106 var p2 = Promise.resolved(p1) |
| 107 var p3 = Promise.resolved(p2) |
| 108 p3.then( |
| 109 function(x) { assertAsync(x === 5, "resolved/then") }, |
| 110 assertUnreachable |
| 111 ) |
| 112 assertAsyncRan() |
| 113 })(); |
| 114 |
| 115 (function() { |
| 116 var p1 = Promise.rejected(5) |
| 117 var p2 = Promise.resolved(p1) |
| 118 var p3 = Promise.resolved(p2) |
| 119 p3.when( |
| 120 function(x) { assertAsync(x === p2, "rejected/when") }, |
| 121 assertUnreachable |
| 122 ) |
| 123 assertAsyncRan() |
| 124 })(); |
| 125 |
| 126 (function() { |
| 127 var p1 = Promise.rejected(5) |
| 128 var p2 = Promise.resolved(p1) |
| 129 var p3 = Promise.resolved(p2) |
| 130 p3.then( |
| 131 assertUnreachable, |
| 132 function(x) { assertAsync(x === 5, "rejected/then") } |
| 133 ) |
| 134 assertAsyncRan() |
| 135 })(); |
| 136 |
| 137 (function() { |
| 138 var p1 = Promise.resolved(5) |
| 139 var p2 = Promise.resolved(p1) |
| 140 var p3 = Promise.resolved(p2) |
| 141 p3.when(function(x) { return x }, assertUnreachable).when( |
| 142 function(x) { assertAsync(x === p1, "resolved/when/when") }, |
| 143 assertUnreachable |
| 144 ) |
| 145 assertAsyncRan() |
| 146 })(); |
| 147 |
| 148 (function() { |
| 149 var p1 = Promise.resolved(5) |
| 150 var p2 = Promise.resolved(p1) |
| 151 var p3 = Promise.resolved(p2) |
| 152 p3.when(function(x) { return x }, assertUnreachable).then( |
| 153 function(x) { assertAsync(x === 5, "resolved/when/then") }, |
| 154 assertUnreachable |
| 155 ) |
| 156 assertAsyncRan() |
| 157 })(); |
| 158 |
| 159 (function() { |
| 160 var p1 = Promise.resolved(5) |
| 161 var p2 = Promise.resolved(p1) |
| 162 var p3 = Promise.resolved(p2) |
| 163 p3.when(function(x) { return 6 }, assertUnreachable).when( |
| 164 function(x) { assertAsync(x === 6, "resolved/when/when2") }, |
| 165 assertUnreachable |
| 166 ) |
| 167 assertAsyncRan() |
| 168 })(); |
| 169 |
| 170 (function() { |
| 171 var p1 = Promise.resolved(5) |
| 172 var p2 = Promise.resolved(p1) |
| 173 var p3 = Promise.resolved(p2) |
| 174 p3.when(function(x) { return 6 }, assertUnreachable).then( |
| 175 function(x) { assertAsync(x === 6, "resolved/when/then2") }, |
| 176 assertUnreachable |
| 177 ) |
| 178 assertAsyncRan() |
| 179 })(); |
| 180 |
| 181 (function() { |
| 182 var p1 = Promise.resolved(5) |
| 183 var p2 = Promise.resolved(p1) |
| 184 var p3 = Promise.resolved(p2) |
| 185 p3.then(function(x) { return x + 1 }, assertUnreachable).when( |
| 186 function(x) { assertAsync(x === 6, "resolved/then/when") }, |
| 187 assertUnreachable |
| 188 ) |
| 189 assertAsyncRan() |
| 190 })(); |
| 191 |
| 192 (function() { |
| 193 var p1 = Promise.resolved(5) |
| 194 var p2 = Promise.resolved(p1) |
| 195 var p3 = Promise.resolved(p2) |
| 196 p3.then(function(x) { return x + 1 }, assertUnreachable).then( |
| 197 function(x) { assertAsync(x === 6, "resolved/then/then") }, |
| 198 assertUnreachable |
| 199 ) |
| 200 assertAsyncRan() |
| 201 })(); |
| 202 |
| 203 (function() { |
| 204 var p1 = Promise.resolved(5) |
| 205 var p2 = Promise.resolved(p1) |
| 206 var p3 = Promise.resolved(p2) |
| 207 p3.then(function(x) { return Promise.resolved(x+1) }, assertUnreachable).when( |
| 208 function(x) { assertAsync(x === 6, "resolved/then/when2") }, |
| 209 assertUnreachable |
| 210 ) |
| 211 assertAsyncRan() |
| 212 })(); |
| 213 |
| 214 (function() { |
| 215 var p1 = Promise.resolved(5) |
| 216 var p2 = Promise.resolved(p1) |
| 217 var p3 = Promise.resolved(p2) |
| 218 p3.then(function(x) { return Promise.resolved(x+1) }, assertUnreachable).then( |
| 219 function(x) { assertAsync(x === 6, "resolved/then/then2") }, |
| 220 assertUnreachable |
| 221 ) |
| 222 assertAsyncRan() |
| 223 })(); |
| 224 |
| 225 (function() { |
| 226 var p1 = Promise.resolved(5) |
| 227 var p2 = Promise.resolved(p1) |
| 228 var p3 = Promise.resolved(p2) |
| 229 p3.when(function(x) { throw 6 }, assertUnreachable).when( |
| 230 assertUnreachable, |
| 231 function(x) { assertAsync(x === 6, "resolved/when-throw/when") } |
| 232 ) |
| 233 assertAsyncRan() |
| 234 })(); |
| 235 |
| 236 (function() { |
| 237 var p1 = Promise.resolved(5) |
| 238 var p2 = Promise.resolved(p1) |
| 239 var p3 = Promise.resolved(p2) |
| 240 p3.when(function(x) { throw 6 }, assertUnreachable).then( |
| 241 assertUnreachable, |
| 242 function(x) { assertAsync(x === 6, "resolved/when-throw/then") } |
| 243 ) |
| 244 assertAsyncRan() |
| 245 })(); |
| 246 |
| 247 (function() { |
| 248 var p1 = Promise.resolved(5) |
| 249 var p2 = Promise.resolved(p1) |
| 250 var p3 = Promise.resolved(p2) |
| 251 p3.then(function(x) { throw 6 }, assertUnreachable).when( |
| 252 assertUnreachable, |
| 253 function(x) { assertAsync(x === 6, "resolved/then-throw/when") } |
| 254 ) |
| 255 assertAsyncRan() |
| 256 })(); |
| 257 |
| 258 (function() { |
| 259 var p1 = Promise.resolved(5) |
| 260 var p2 = Promise.resolved(p1) |
| 261 var p3 = Promise.resolved(p2) |
| 262 p3.then(function(x) { throw 6 }, assertUnreachable).then( |
| 263 assertUnreachable, |
| 264 function(x) { assertAsync(x === 6, "resolved/then-throw/then") } |
| 265 ) |
| 266 assertAsyncRan() |
| 267 })(); |
| 268 |
| 269 (function() { |
| 270 var p1 = Promise.resolved(5) |
| 271 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 272 var p3 = Promise.resolved(p2) |
| 273 p3.when( |
| 274 function(x) { assertAsync(x === p2, "resolved/thenable/when") }, |
| 275 assertUnreachable |
| 276 ) |
| 277 assertAsyncRan() |
| 278 })(); |
| 279 |
| 280 (function() { |
| 281 var p1 = Promise.resolved(5) |
| 282 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 283 var p3 = Promise.resolved(p2) |
| 284 p3.then( |
| 285 function(x) { assertAsync(x === 5, "resolved/thenable/then") }, |
| 286 assertUnreachable |
| 287 ) |
| 288 assertAsyncRan() |
| 289 })(); |
| 290 |
| 291 (function() { |
| 292 var p1 = Promise.rejected(5) |
| 293 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 294 var p3 = Promise.resolved(p2) |
| 295 p3.when( |
| 296 function(x) { assertAsync(x === p2, "rejected/thenable/when") }, |
| 297 assertUnreachable |
| 298 ) |
| 299 assertAsyncRan() |
| 300 })(); |
| 301 |
| 302 (function() { |
| 303 var p1 = Promise.rejected(5) |
| 304 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 305 var p3 = Promise.resolved(p2) |
| 306 p3.then( |
| 307 assertUnreachable, |
| 308 function(x) { assertAsync(x === 5, "rejected/thenable/then") } |
| 309 ) |
| 310 assertAsyncRan() |
| 311 })(); |
| 312 |
| 313 (function() { |
| 314 var deferred = Promise.deferred() |
| 315 var p1 = deferred.promise |
| 316 var p2 = Promise.resolved(p1) |
| 317 var p3 = Promise.resolved(p2) |
| 318 p3.when( |
| 319 function(x) { assertAsync(x === p2, "when/resolve") }, |
| 320 assertUnreachable |
| 321 ) |
| 322 deferred.resolve(5) |
| 323 assertAsyncRan() |
| 324 })(); |
| 325 |
| 326 (function() { |
| 327 var deferred = Promise.deferred() |
| 328 var p1 = deferred.promise |
| 329 var p2 = Promise.resolved(p1) |
| 330 var p3 = Promise.resolved(p2) |
| 331 p3.then( |
| 332 function(x) { assertAsync(x === 5, "then/resolve") }, |
| 333 assertUnreachable |
| 334 ) |
| 335 deferred.resolve(5) |
| 336 assertAsyncRan() |
| 337 })(); |
| 338 |
| 339 (function() { |
| 340 var deferred = Promise.deferred() |
| 341 var p1 = deferred.promise |
| 342 var p2 = Promise.resolved(p1) |
| 343 var p3 = Promise.resolved(p2) |
| 344 p3.when( |
| 345 function(x) { assertAsync(x === p2, "when/reject") }, |
| 346 assertUnreachable |
| 347 ) |
| 348 deferred.reject(5) |
| 349 assertAsyncRan() |
| 350 })(); |
| 351 |
| 352 (function() { |
| 353 var deferred = Promise.deferred() |
| 354 var p1 = deferred.promise |
| 355 var p2 = Promise.resolved(p1) |
| 356 var p3 = Promise.resolved(p2) |
| 357 p3.then( |
| 358 assertUnreachable, |
| 359 function(x) { assertAsync(x === 5, "then/reject") } |
| 360 ) |
| 361 deferred.reject(5) |
| 362 assertAsyncRan() |
| 363 })(); |
| 364 |
| 365 (function() { |
| 366 var deferred = Promise.deferred() |
| 367 var p1 = deferred.promise |
| 368 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 369 var p3 = Promise.resolved(p2) |
| 370 p3.when( |
| 371 function(x) { assertAsync(x === p2, "when/resolve/thenable") }, |
| 372 assertUnreachable |
| 373 ) |
| 374 deferred.resolve(5) |
| 375 assertAsyncRan() |
| 376 })(); |
| 377 |
| 378 (function() { |
| 379 var deferred = Promise.deferred() |
| 380 var p1 = deferred.promise |
| 381 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 382 var p3 = Promise.resolved(p2) |
| 383 p3.then( |
| 384 function(x) { assertAsync(x === 5, "then/resolve/thenable") }, |
| 385 assertUnreachable |
| 386 ) |
| 387 deferred.resolve(5) |
| 388 assertAsyncRan() |
| 389 })(); |
| 390 |
| 391 (function() { |
| 392 var deferred = Promise.deferred() |
| 393 var p1 = deferred.promise |
| 394 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 395 var p3 = Promise.resolved(p2) |
| 396 p3.when( |
| 397 function(x) { assertAsync(x === p2, "when/reject/thenable") }, |
| 398 assertUnreachable |
| 399 ) |
| 400 deferred.reject(5) |
| 401 assertAsyncRan() |
| 402 })(); |
| 403 |
| 404 (function() { |
| 405 var deferred = Promise.deferred() |
| 406 var p1 = deferred.promise |
| 407 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 408 var p3 = Promise.resolved(p2) |
| 409 p3.then( |
| 410 assertUnreachable, |
| 411 function(x) { assertAsync(x === 5, "then/reject/thenable") } |
| 412 ) |
| 413 deferred.reject(5) |
| 414 assertAsyncRan() |
| 415 })(); |
| 416 |
| 417 (function() { |
| 418 var p1 = Promise.resolved(5) |
| 419 var p2 = Promise.resolved(p1) |
| 420 var deferred = Promise.deferred() |
| 421 var p3 = deferred.promise |
| 422 p3.when( |
| 423 function(x) { assertAsync(x === p2, "when/resolve2") }, |
| 424 assertUnreachable |
| 425 ) |
| 426 deferred.resolve(p2) |
| 427 assertAsyncRan() |
| 428 })(); |
| 429 |
| 430 (function() { |
| 431 var p1 = Promise.resolved(5) |
| 432 var p2 = Promise.resolved(p1) |
| 433 var deferred = Promise.deferred() |
| 434 var p3 = deferred.promise |
| 435 p3.then( |
| 436 function(x) { assertAsync(x === 5, "then/resolve2") }, |
| 437 assertUnreachable |
| 438 ) |
| 439 deferred.resolve(p2) |
| 440 assertAsyncRan() |
| 441 })(); |
| 442 |
| 443 (function() { |
| 444 var p1 = Promise.resolved(5) |
| 445 var p2 = Promise.resolved(p1) |
| 446 var deferred = Promise.deferred() |
| 447 var p3 = deferred.promise |
| 448 p3.when( |
| 449 assertUnreachable, |
| 450 function(x) { assertAsync(x === 5, "when/reject2") } |
| 451 ) |
| 452 deferred.reject(5) |
| 453 assertAsyncRan() |
| 454 })(); |
| 455 |
| 456 (function() { |
| 457 var p1 = Promise.resolved(5) |
| 458 var p2 = Promise.resolved(p1) |
| 459 var deferred = Promise.deferred() |
| 460 var p3 = deferred.promise |
| 461 p3.then( |
| 462 assertUnreachable, |
| 463 function(x) { assertAsync(x === 5, "then/reject2") } |
| 464 ) |
| 465 deferred.reject(5) |
| 466 assertAsyncRan() |
| 467 })(); |
| 468 |
| 469 (function() { |
| 470 var p1 = Promise.resolved(5) |
| 471 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 472 var deferred = Promise.deferred() |
| 473 var p3 = deferred.promise |
| 474 p3.when( |
| 475 function(x) { assertAsync(x === p2, "when/resolve/thenable2") }, |
| 476 assertUnreachable |
| 477 ) |
| 478 deferred.resolve(p2) |
| 479 assertAsyncRan() |
| 480 })(); |
| 481 |
| 482 (function() { |
| 483 var p1 = Promise.resolved(5) |
| 484 var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} |
| 485 var deferred = Promise.deferred() |
| 486 var p3 = deferred.promise |
| 487 p3.then( |
| 488 function(x) { assertAsync(x === 5, "then/resolve/thenable2") }, |
| 489 assertUnreachable |
| 490 ) |
| 491 deferred.resolve(p2) |
| 492 assertAsyncRan() |
| 493 })(); |
| 494 |
| 495 (function() { |
| 496 var p1 = Promise.resolved(0) |
| 497 var p2 = p1.when(function(x) { return p2 }, assertUnreachable) |
| 498 p2.when( |
| 499 assertUnreachable, |
| 500 function(r) { assertAsync(r instanceof TypeError, "cyclic/when") } |
| 501 ) |
| 502 assertAsyncRan() |
| 503 })(); |
| 504 |
| 505 (function() { |
| 506 var p1 = Promise.resolved(0) |
| 507 var p2 = p1.then(function(x) { return p2 }, assertUnreachable) |
| 508 p2.when( |
| 509 assertUnreachable, |
| 510 function(r) { assertAsync(r instanceof TypeError, "cyclic/then") } |
| 511 ) |
| 512 assertAsyncRan() |
| 513 })(); |
| 514 |
| 515 (function() { |
| 516 var deferred = Promise.deferred() |
| 517 var p = deferred.promise |
| 518 deferred.resolve(p) |
| 519 p.when( |
| 520 function(x) { assertAsync(x === p, "cyclic/deferred/when") }, |
| 521 assertUnreachable |
| 522 ) |
| 523 assertAsyncRan() |
| 524 })(); |
| 525 |
| 526 (function() { |
| 527 var deferred = Promise.deferred() |
| 528 var p = deferred.promise |
| 529 deferred.resolve(p) |
| 530 p.then( |
| 531 assertUnreachable, |
| 532 function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then") } |
| 533 ) |
| 534 assertAsyncRan() |
| 535 })(); |
| 536 |
| 537 (function() { |
| 538 var deferred1 = Promise.deferred() |
| 539 var p1 = deferred1.promise |
| 540 var deferred2 = Promise.deferred() |
| 541 var p2 = deferred2.promise |
| 542 var deferred3 = Promise.deferred() |
| 543 var p3 = deferred3.promise |
| 544 Promise.all([p1, p2, p3]).when( |
| 545 function(x) { assertAsync(x === undefined, "all/resolve") }, |
| 546 assertUnreachable |
| 547 ) |
| 548 deferred1.resolve(1) |
| 549 deferred3.resolve(3) |
| 550 deferred2.resolve(2) |
| 551 assertAsyncRan() |
| 552 })(); |
| 553 |
| 554 (function() { |
| 555 var deferred = Promise.deferred() |
| 556 var p1 = deferred.promise |
| 557 var p2 = Promise.resolved(2) |
| 558 var p3 = Promise.deferred().promise |
| 559 Promise.all([p1, p2, p3]).when( |
| 560 assertUnreachable, |
| 561 assertUnreachable |
| 562 ) |
| 563 deferred.resolve(1) |
| 564 })(); |
| 565 |
| 566 (function() { |
| 567 var deferred1 = Promise.deferred() |
| 568 var p1 = deferred1.promise |
| 569 var deferred2 = Promise.deferred() |
| 570 var p2 = deferred2.promise |
| 571 var deferred3 = Promise.deferred() |
| 572 var p3 = deferred3.promise |
| 573 Promise.all([p1, p2, p3]).when( |
| 574 assertUnreachable, |
| 575 function(x) { assertAsync(x === 2, "all/reject") } |
| 576 ) |
| 577 deferred1.resolve(1) |
| 578 deferred3.resolve(3) |
| 579 deferred2.reject(2) |
| 580 assertAsyncRan() |
| 581 })(); |
| 582 |
| 583 (function() { |
| 584 var p1 = Promise.resolved(1) |
| 585 var p2 = Promise.resolved(2) |
| 586 var p3 = Promise.resolved(3) |
| 587 Promise.one([p1, p2, p3]).when( |
| 588 function(x) { assertAsync(x === 1, "resolved/all") }, |
| 589 assertUnreachable |
| 590 ) |
| 591 assertAsyncRan() |
| 592 })(); |
| 593 |
| 594 (function() { |
| 595 var p1 = Promise.resolved(1) |
| 596 var p2 = Promise.resolved(2) |
| 597 var p3 = Promise.resolved(3) |
| 598 Promise.one([0, p1, p2, p3]).when( |
| 599 function(x) { assertAsync(x === 0, "resolved-const/all") }, |
| 600 assertUnreachable |
| 601 ) |
| 602 assertAsyncRan() |
| 603 })(); |
| 604 |
| 605 (function() { |
| 606 var deferred1 = Promise.deferred() |
| 607 var p1 = deferred1.promise |
| 608 var deferred2 = Promise.deferred() |
| 609 var p2 = deferred2.promise |
| 610 var deferred3 = Promise.deferred() |
| 611 var p3 = deferred3.promise |
| 612 Promise.one([p1, p2, p3]).when( |
| 613 function(x) { assertAsync(x === 3, "one/resolve") }, |
| 614 assertUnreachable |
| 615 ) |
| 616 deferred3.resolve(3) |
| 617 deferred1.resolve(1) |
| 618 assertAsyncRan() |
| 619 })(); |
| 620 |
| 621 (function() { |
| 622 var deferred = Promise.deferred() |
| 623 var p1 = deferred.promise |
| 624 var p2 = Promise.resolved(2) |
| 625 var p3 = Promise.deferred().promise |
| 626 Promise.one([p1, p2, p3]).when( |
| 627 function(x) { assertAsync(x === 2, "resolved/one") }, |
| 628 assertUnreachable |
| 629 ) |
| 630 deferred.resolve(1) |
| 631 assertAsyncRan() |
| 632 })(); |
| 633 |
| 634 (function() { |
| 635 var deferred1 = Promise.deferred() |
| 636 var p1 = deferred1.promise |
| 637 var deferred2 = Promise.deferred() |
| 638 var p2 = deferred2.promise |
| 639 var deferred3 = Promise.deferred() |
| 640 var p3 = deferred3.promise |
| 641 Promise.one([p1, p2, p3]).when( |
| 642 function(x) { assertAsync(x === 3, "one/resolve/reject") }, |
| 643 assertUnreachable |
| 644 ) |
| 645 deferred3.resolve(3) |
| 646 deferred1.reject(1) |
| 647 assertAsyncRan() |
| 648 })(); |
| 649 |
| 650 (function() { |
| 651 var deferred1 = Promise.deferred() |
| 652 var p1 = deferred1.promise |
| 653 var deferred2 = Promise.deferred() |
| 654 var p2 = deferred2.promise |
| 655 var deferred3 = Promise.deferred() |
| 656 var p3 = deferred3.promise |
| 657 Promise.one([p1, p2, p3]).when( |
| 658 assertUnreachable, |
| 659 function(x) { assertAsync(x === 3, "one/reject/resolve") } |
| 660 ) |
| 661 deferred3.reject(3) |
| 662 deferred1.resolve(1) |
| 663 assertAsyncRan() |
| 664 })(); |
| 665 |
| 666 (function() { |
| 667 var log |
| 668 function MyPromise(resolver) { |
| 669 log += "n" |
| 670 Promise.call(this, |
| 671 function(resolve, reject) { |
| 672 resolver( |
| 673 function(x) { log += "x" + x; resolve(x) }, |
| 674 function(r) { log += "r" + r; reject(r) } |
| 675 ) |
| 676 } |
| 677 ) |
| 678 } |
| 679 |
| 680 MyPromise.__proto__ = Promise |
| 681 MyPromise.deferred = function() { |
| 682 log += "d" |
| 683 return this.__proto__.deferred.call(this) |
| 684 } |
| 685 |
| 686 MyPromise.prototype.__proto__ = Promise.prototype |
| 687 MyPromise.prototype.when = function(resolve, reject) { |
| 688 log += "w" |
| 689 return this.__proto__.__proto__.when.call(this, resolve, reject) |
| 690 } |
| 691 |
| 692 log = "" |
| 693 var p1 = new MyPromise(function(resolve, reject) { resolve(1) }) |
| 694 var p2 = new MyPromise(function(resolve, reject) { reject(2) }) |
| 695 var d3 = MyPromise.deferred() |
| 696 assertTrue(d3.promise instanceof MyPromise, "subclass/instance3") |
| 697 assertTrue(log === "nx1nr2dn", "subclass/create") |
| 698 |
| 699 log = "" |
| 700 var p4 = MyPromise.resolved(4) |
| 701 var p5 = MyPromise.rejected(5) |
| 702 assertTrue(p4 instanceof MyPromise, "subclass/instance4") |
| 703 assertTrue(p5 instanceof MyPromise, "subclass/instance5") |
| 704 d3.resolve(3) |
| 705 assertTrue(log === "nx4nr5x3", "subclass/resolve") |
| 706 |
| 707 log = "" |
| 708 var d6 = MyPromise.deferred() |
| 709 d6.promise.when(function(x) { return new Promise(x) }).when(function() {}) |
| 710 d6.resolve(6) |
| 711 assertTrue(log === "dnwnwnx6", "subclass/when") |
| 712 |
| 713 log = "" |
| 714 Promise.all([11, Promise.resolved(12), 13, MyPromise.resolved(14), 15, 16]) |
| 715 assertTrue(log === "nx14wn", "subclass/all/arg") |
| 716 |
| 717 log = "" |
| 718 MyPromise.all([21, Promise.resolved(22), 23, MyPromise.resolved(24), 25, 26]) |
| 719 assertTrue(log === "nx24dnnx21wnnx23wnwnnx25wnnx26wn", "subclass/all/self") |
| 720 })(); |
| 721 |
| 722 |
| 723 assertAsyncDone() |
OLD | NEW |