Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --harmony-promise-finally --allow-natives-syntax | |
| 6 | |
| 7 var asyncAssertsExpected = 0; | |
| 8 | |
| 9 function assertUnreachable() { | |
| 10 %AbortJS("Unreachable: failure"); | |
| 11 } | |
| 12 | |
| 13 function assertAsyncRan() { | |
| 14 ++asyncAssertsExpected; | |
| 15 } | |
| 16 | |
| 17 function assertAsync(b, s) { | |
| 18 if (b) { | |
| 19 print(s, "succeeded"); | |
| 20 } else { | |
| 21 %AbortJS(s + " FAILED!"); | |
| 22 } | |
| 23 --asyncAssertsExpected; | |
| 24 } | |
| 25 | |
| 26 function assertEqualsAsync(b, s) { | |
| 27 if (b === s) { | |
| 28 print(b, "===", s, "succeeded"); | |
| 29 } else { | |
| 30 %AbortJS(b + "===" + s + " FAILED!"); | |
| 31 } | |
| 32 --asyncAssertsExpected; | |
| 33 } | |
| 34 | |
| 35 function assertAsyncDone(iteration) { | |
| 36 var iteration = iteration || 0; | |
| 37 %EnqueueMicrotask(function() { | |
| 38 if (asyncAssertsExpected === 0) | |
| 39 assertAsync(true, "all"); | |
| 40 else if ( | |
| 41 iteration > 10 // Shouldn't take more. | |
| 42 ) | |
| 43 assertAsync(false, "all... " + asyncAssertsExpected); | |
| 44 else | |
| 45 assertAsyncDone(iteration + 1); | |
| 46 }); | |
| 47 } | |
| 48 | |
| 49 (function() { | |
| 50 assertThrows( | |
| 51 function() { | |
| 52 Promise.prototype.finally.call(5); | |
| 53 }, | |
| 54 TypeError | |
| 55 ); | |
| 56 })(); | |
| 57 | |
| 58 // resolve/finally/then | |
| 59 (function() { | |
| 60 Promise.resolve(3).finally().then( | |
| 61 x => { | |
| 62 assertEqualsAsync(3, x); | |
| 63 }, | |
| 64 assertUnreachable | |
| 65 ); | |
| 66 assertAsyncRan(); | |
| 67 })(); | |
| 68 | |
| 69 // reject/finally/then | |
| 70 (function() { | |
| 71 Promise.reject(3).finally().then(assertUnreachable, x => { | |
| 72 assertEqualsAsync(3, x); | |
| 73 }); | |
| 74 assertAsyncRan(); | |
| 75 })(); | |
| 76 | |
| 77 // resolve/finally-return-notcallable/then | |
| 78 (function() { | |
| 79 Promise.resolve(3).finally(2).then( | |
| 80 x => { | |
| 81 assertEqualsAsync(3, x); | |
| 82 }, | |
| 83 assertUnreachable | |
| 84 ); | |
| 85 assertAsyncRan(); | |
| 86 })(); | |
| 87 | |
| 88 // reject/finally-return-notcallable/then | |
| 89 (function() { | |
| 90 Promise.reject(3).finally(2).then( | |
| 91 assertUnreachable, e => { | |
| 92 assertEqualsAsync(3, e); | |
| 93 }); | |
| 94 assertAsyncRan(); | |
| 95 })(); | |
| 96 | |
| 97 // reject/finally/catch | |
| 98 (function() { | |
| 99 Promise.reject(3).finally().catch(reason => { | |
| 100 assertEqualsAsync(3, reason); | |
| 101 }); | |
| 102 assertAsyncRan(); | |
| 103 })(); | |
| 104 | |
| 105 // reject/finally/then/catch | |
| 106 (function() { | |
| 107 Promise.reject(3).finally().then(assertUnreachable).catch(reason => { | |
| 108 assertEqualsAsync(3, reason); | |
| 109 }); | |
| 110 assertAsyncRan(); | |
| 111 })(); | |
| 112 | |
| 113 // resolve/then/finally/then | |
| 114 (function() { | |
| 115 Promise.resolve(3) | |
| 116 .then(x => { | |
| 117 assertEqualsAsync(3, x); | |
| 118 return x; | |
| 119 }) | |
|
neis
2017/02/17 12:30:16
here
gsathya
2017/02/17 12:54:24
i'm keeping this. there's no other test for resolv
| |
| 120 .finally() | |
| 121 .then( | |
| 122 x => { | |
| 123 assertEqualsAsync(3, x); | |
| 124 }, | |
| 125 assertUnreachable | |
| 126 ); | |
| 127 assertAsyncRan(); | |
| 128 assertAsyncRan(); | |
| 129 })(); | |
| 130 | |
| 131 // reject/catch/finally/then | |
| 132 (function() { | |
| 133 Promise.reject(3) | |
| 134 .catch(x => { | |
| 135 assertEqualsAsync(3, x); | |
| 136 return x; | |
| 137 }) | |
| 138 .finally() | |
| 139 .then( | |
| 140 x => { | |
| 141 assertEqualsAsync(3, x); | |
| 142 }, | |
| 143 assertUnreachable | |
| 144 ); | |
| 145 assertAsyncRan(); | |
| 146 assertAsyncRan(); | |
| 147 })(); | |
| 148 | |
| 149 // resolve/finally-throw/then | |
| 150 (function() { | |
| 151 Promise.resolve(3) | |
| 152 .finally(function onFinally() { | |
| 153 assertEqualsAsync(0, arguments.length); | |
| 154 throw 1; | |
| 155 }) | |
| 156 .then(assertUnreachable, function onRejected(reason) { | |
| 157 assertEqualsAsync(1, reason); | |
| 158 }); | |
| 159 assertAsyncRan(); | |
| 160 assertAsyncRan(); | |
| 161 })(); | |
| 162 | |
| 163 // reject/finally-throw/then | |
| 164 (function() { | |
| 165 Promise.reject(3) | |
| 166 .finally(function onFinally() { | |
| 167 assertEqualsAsync(0, arguments.length); | |
| 168 throw 1; | |
| 169 }) | |
| 170 .then(assertUnreachable, function onRejected(reason) { | |
| 171 assertEqualsAsync(1, reason); | |
| 172 }); | |
| 173 assertAsyncRan(); | |
| 174 assertAsyncRan(); | |
| 175 })(); | |
| 176 | |
| 177 // resolve/finally-return/then | |
| 178 (function() { | |
| 179 Promise.resolve(3) | |
| 180 .finally(function onFinally() { | |
| 181 assertEqualsAsync(0, arguments.length); | |
| 182 return 4; | |
| 183 }) | |
| 184 .then( | |
| 185 x => { | |
| 186 assertEqualsAsync(x, 3); | |
| 187 }, | |
| 188 assertUnreachable | |
| 189 ); | |
| 190 assertAsyncRan(); | |
| 191 assertAsyncRan(); | |
| 192 })(); | |
| 193 | |
| 194 // reject/finally-return/then | |
| 195 (function() { | |
| 196 Promise.reject(3) | |
| 197 .finally(function onFinally() { | |
| 198 assertEqualsAsync(0, arguments.length); | |
| 199 return 4; | |
| 200 }) | |
| 201 .then(assertUnreachable, x => { | |
| 202 assertEqualsAsync(x, 3); | |
| 203 }); | |
| 204 assertAsyncRan(); | |
| 205 assertAsyncRan(); | |
| 206 })(); | |
| 207 | |
| 208 // reject/catch-throw/finally-throw/then | |
| 209 (function() { | |
| 210 Promise.reject(3) | |
| 211 .catch(e => { | |
| 212 assertEqualsAsync(3, e); | |
| 213 throw e; | |
| 214 }) | |
| 215 .finally(function onFinally() { | |
| 216 assertEqualsAsync(0, arguments.length); | |
| 217 throw 4; | |
| 218 }) | |
| 219 .then(assertUnreachable, function onRejected(e) { | |
| 220 assertEqualsAsync(4, e); | |
| 221 }); | |
| 222 assertAsyncRan(); | |
| 223 assertAsyncRan(); | |
| 224 assertAsyncRan(); | |
| 225 })(); | |
| 226 | |
| 227 // resolve/then-throw/finally-throw/then | |
| 228 (function() { | |
| 229 Promise.resolve(3) | |
| 230 .then(e => { | |
| 231 assertEqualsAsync(3, e); | |
| 232 throw e; | |
| 233 }) | |
|
neis
2017/02/17 12:30:16
here
neis
2017/02/17 12:31:51
keep this one if you like
gsathya
2017/02/17 12:54:24
keeping this because the then throws
| |
| 234 .finally(function onFinally() { | |
| 235 assertEqualsAsync(0, arguments.length); | |
| 236 throw 4; | |
| 237 }) | |
| 238 .then(assertUnreachable, function onRejected(e) { | |
| 239 assertEqualsAsync(4, e); | |
| 240 }); | |
| 241 assertAsyncRan(); | |
| 242 assertAsyncRan(); | |
| 243 assertAsyncRan(); | |
| 244 })(); | |
| 245 | |
| 246 // resolve/finally-return-rejected-promise/then | |
| 247 (function() { | |
| 248 Promise.resolve(3) | |
| 249 .finally(function onFinally() { | |
| 250 assertEqualsAsync(0, arguments.length); | |
| 251 return Promise.reject(4); | |
| 252 }) | |
| 253 .then(assertUnreachable, e => { | |
| 254 assertEqualsAsync(4, e); | |
| 255 }); | |
| 256 assertAsyncRan(); | |
| 257 assertAsyncRan(); | |
| 258 })(); | |
| 259 | |
| 260 // reject/finally-return-rejected-promise/then | |
| 261 (function() { | |
| 262 Promise.reject(3) | |
| 263 .finally(function onFinally() { | |
| 264 assertEqualsAsync(0, arguments.length); | |
| 265 return Promise.reject(4); | |
| 266 }) | |
| 267 .then(assertUnreachable, e => { | |
| 268 assertEqualsAsync(4, e); | |
| 269 }); | |
| 270 assertAsyncRan(); | |
| 271 assertAsyncRan(); | |
| 272 })(); | |
| 273 | |
| 274 // resolve/finally-return-resolved-promise/then | |
| 275 (function() { | |
| 276 Promise.resolve(3) | |
| 277 .finally(function onFinally() { | |
| 278 assertEqualsAsync(0, arguments.length); | |
| 279 return Promise.resolve(4); | |
| 280 }) | |
| 281 .then( | |
| 282 x => { | |
| 283 assertEqualsAsync(3, x); | |
| 284 }, | |
| 285 assertUnreachable | |
| 286 ); | |
| 287 assertAsyncRan(); | |
| 288 assertAsyncRan(); | |
| 289 })(); | |
| 290 | |
| 291 // reject/finally-return-resolved-promise/then | |
| 292 (function() { | |
| 293 Promise.reject(3) | |
| 294 .finally(function onFinally() { | |
| 295 assertEqualsAsync(0, arguments.length); | |
| 296 return Promise.resolve(4); | |
| 297 }) | |
| 298 .then(assertUnreachable, e => { | |
| 299 assertEqualsAsync(3, e); | |
| 300 }); | |
| 301 assertAsyncRan(); | |
| 302 assertAsyncRan(); | |
| 303 })(); | |
| 304 | |
| 305 // reject/finally-return-resolved-promise/then | |
| 306 (function() { | |
| 307 Promise.reject(3) | |
| 308 .finally(function onFinally() { | |
| 309 assertEqualsAsync(0, arguments.length); | |
| 310 return Promise.resolve(4); | |
| 311 }) | |
| 312 .then(assertUnreachable, e => { | |
| 313 assertEqualsAsync(3, e); | |
| 314 }); | |
| 315 assertAsyncRan(); | |
| 316 assertAsyncRan(); | |
| 317 })(); | |
| 318 | |
| 319 // resolve/finally-thenable-resolve/then | |
| 320 (function() { | |
| 321 var thenable = { | |
| 322 then: function(onResolve, onReject) { | |
| 323 onResolve(5); | |
| 324 } | |
| 325 }; | |
| 326 | |
| 327 Promise.resolve(5) | |
| 328 .finally(function onFinally() { | |
| 329 assertEqualsAsync(0, arguments.length); | |
| 330 return thenable; | |
| 331 }) | |
| 332 .then( | |
| 333 x => { | |
| 334 assertEqualsAsync(5, x); | |
| 335 }, | |
| 336 assertUnreachable | |
| 337 ); | |
| 338 | |
| 339 assertAsyncRan(); | |
| 340 assertAsyncRan(); | |
| 341 })(); | |
| 342 | |
| 343 // reject/finally-thenable-resolve/then | |
| 344 (function() { | |
| 345 var thenable = { | |
| 346 then: function(onResolve, onReject) { | |
| 347 onResolve(1); | |
| 348 } | |
| 349 }; | |
| 350 | |
| 351 Promise.reject(5) | |
| 352 .finally(function onFinally() { | |
| 353 assertEqualsAsync(0, arguments.length); | |
| 354 return thenable; | |
| 355 }) | |
| 356 .then(assertUnreachable, e => { | |
| 357 assertEqualsAsync(5, e); | |
| 358 }); | |
| 359 | |
| 360 assertAsyncRan(); | |
| 361 assertAsyncRan(); | |
| 362 })(); | |
| 363 | |
| 364 // reject/finally-thenable-reject/then | |
| 365 (function() { | |
| 366 var thenable = { | |
| 367 then: function(onResolve, onReject) { | |
| 368 onReject(1); | |
| 369 } | |
| 370 }; | |
| 371 | |
| 372 Promise.reject(5) | |
| 373 .finally(function onFinally() { | |
| 374 assertEqualsAsync(0, arguments.length); | |
| 375 return thenable; | |
| 376 }) | |
| 377 .then(assertUnreachable, e => { | |
| 378 assertEqualsAsync(1, e); | |
| 379 }); | |
| 380 | |
| 381 assertAsyncRan(); | |
| 382 assertAsyncRan(); | |
| 383 })(); | |
| 384 | |
| 385 // resolve/finally-thenable-reject/then | |
| 386 (function() { | |
| 387 var thenable = { | |
| 388 then: function(onResolve, onReject) { | |
| 389 onReject(1); | |
| 390 } | |
| 391 }; | |
| 392 | |
| 393 Promise.resolve(5) | |
| 394 .finally(function onFinally() { | |
| 395 assertEqualsAsync(0, arguments.length); | |
| 396 return thenable; | |
| 397 }) | |
| 398 .then(assertUnreachable, e => { | |
| 399 assertEqualsAsync(1, e); | |
| 400 }); | |
| 401 | |
| 402 assertAsyncRan(); | |
| 403 assertAsyncRan(); | |
| 404 })(); | |
| 405 | |
| 406 // resolve/finally/finally/then | |
| 407 (function() { | |
| 408 Promise.resolve(5) | |
| 409 .finally(function onFinally() { | |
| 410 assertEqualsAsync(0, arguments.length); | |
| 411 }) | |
| 412 .finally(function onFinally() { | |
| 413 assertEqualsAsync(0, arguments.length); | |
| 414 }) | |
| 415 .then( | |
| 416 x => { | |
| 417 assertEqualsAsync(5, x); | |
| 418 }, | |
| 419 assertUnreachable | |
| 420 ); | |
| 421 | |
| 422 assertAsyncRan(); | |
| 423 assertAsyncRan(); | |
| 424 assertAsyncRan(); | |
| 425 })(); | |
| 426 | |
| 427 // resolve/finally-throw/finally/then | |
| 428 (function() { | |
| 429 Promise.resolve(5) | |
| 430 .finally(function onFinally() { | |
| 431 assertEqualsAsync(0, arguments.length); | |
| 432 throw 1; | |
| 433 }) | |
| 434 .finally(function onFinally() { | |
| 435 assertEqualsAsync(0, arguments.length); | |
| 436 }) | |
| 437 .then(assertUnreachable, e => { | |
| 438 assertEqualsAsync(1, e); | |
| 439 }); | |
| 440 | |
| 441 assertAsyncRan(); | |
| 442 assertAsyncRan(); | |
| 443 assertAsyncRan(); | |
| 444 })(); | |
| 445 | |
| 446 // resolve/finally-return-rejected-promise/finally/then | |
| 447 (function() { | |
| 448 Promise.resolve(5) | |
| 449 .finally(function onFinally() { | |
| 450 assertEqualsAsync(0, arguments.length); | |
| 451 return Promise.reject(1); | |
| 452 }) | |
| 453 .finally(function onFinally() { | |
| 454 assertEqualsAsync(0, arguments.length); | |
| 455 }) | |
| 456 .then(assertUnreachable, e => { | |
| 457 assertEqualsAsync(1, e); | |
| 458 }); | |
| 459 | |
| 460 assertAsyncRan(); | |
| 461 assertAsyncRan(); | |
| 462 assertAsyncRan(); | |
| 463 })(); | |
| 464 | |
| 465 // reject/finally/finally/then | |
| 466 (function() { | |
| 467 Promise.reject(5) | |
| 468 .finally(function onFinally() { | |
| 469 assertEqualsAsync(0, arguments.length); | |
| 470 }) | |
| 471 .finally(function onFinally() { | |
| 472 assertEqualsAsync(0, arguments.length); | |
| 473 }) | |
| 474 .then(assertUnreachable, e => { | |
| 475 assertEqualsAsync(5, e); | |
| 476 }); | |
| 477 | |
| 478 assertAsyncRan(); | |
| 479 assertAsyncRan(); | |
| 480 assertAsyncRan(); | |
| 481 })(); | |
| 482 | |
| 483 // reject/finally-throw/finally/then | |
| 484 (function() { | |
| 485 Promise.reject(5) | |
| 486 .finally(function onFinally() { | |
| 487 assertEqualsAsync(0, arguments.length); | |
| 488 throw 1; | |
| 489 }) | |
| 490 .finally(function onFinally() { | |
| 491 assertEqualsAsync(0, arguments.length); | |
| 492 }) | |
| 493 .then(assertUnreachable, e => { | |
| 494 assertEqualsAsync(1, e); | |
| 495 }); | |
| 496 | |
| 497 assertAsyncRan(); | |
| 498 assertAsyncRan(); | |
| 499 assertAsyncRan(); | |
| 500 })(); | |
| 501 | |
| 502 // reject/finally-return-rejected-promise/finally/then | |
| 503 (function() { | |
| 504 Promise.reject(5) | |
| 505 .finally(function onFinally() { | |
| 506 assertEqualsAsync(0, arguments.length); | |
| 507 return Promise.reject(1); | |
| 508 }) | |
| 509 .finally(function onFinally() { | |
| 510 assertEqualsAsync(0, arguments.length); | |
| 511 }) | |
| 512 .then(assertUnreachable, e => { | |
| 513 assertEqualsAsync(1, e); | |
| 514 }); | |
| 515 | |
| 516 assertAsyncRan(); | |
| 517 assertAsyncRan(); | |
| 518 assertAsyncRan(); | |
| 519 })(); | |
| 520 | |
| 521 // resolve/finally-deferred-resolve/then | |
| 522 (function() { | |
| 523 var resolve, reject; | |
| 524 var deferred = new Promise((x, y) => { | |
| 525 resolve = x; | |
| 526 reject = y; | |
| 527 }); | |
| 528 Promise.resolve(1) | |
| 529 .finally(function onFinally() { | |
| 530 assertEqualsAsync(0, arguments.length); | |
| 531 return deferred; | |
| 532 }) | |
| 533 .then( | |
| 534 x => { | |
| 535 assertEqualsAsync(1, x); | |
| 536 }, | |
| 537 assertUnreachable | |
| 538 ); | |
| 539 | |
| 540 assertAsyncRan(); | |
| 541 assertAsyncRan(); | |
| 542 | |
| 543 resolve(5); | |
| 544 })(); | |
| 545 | |
| 546 // resolve/finally-deferred-reject/then | |
| 547 (function() { | |
| 548 var resolve, reject; | |
| 549 var deferred = new Promise((x, y) => { | |
| 550 resolve = x; | |
| 551 reject = y; | |
| 552 }); | |
| 553 Promise.resolve(1) | |
| 554 .finally(function onFinally() { | |
| 555 assertEqualsAsync(0, arguments.length); | |
| 556 return deferred; | |
| 557 }) | |
| 558 .then(assertUnreachable, e => { | |
| 559 assertEqualsAsync(5, e); | |
| 560 }); | |
| 561 | |
| 562 assertAsyncRan(); | |
| 563 assertAsyncRan(); | |
| 564 | |
| 565 reject(5); | |
| 566 })(); | |
| 567 | |
| 568 // all/finally/then | |
| 569 (function() { | |
| 570 var resolve, reject; | |
| 571 var deferred = new Promise((x, y) => { | |
| 572 resolve = x; | |
| 573 reject = y; | |
| 574 }); | |
| 575 | |
| 576 Promise.all([deferred]) | |
| 577 .finally(function onFinally() { | |
| 578 assertEqualsAsync(0, arguments.length); | |
| 579 }) | |
| 580 .then( | |
| 581 ([x]) => { | |
| 582 assertEqualsAsync(1, x); | |
| 583 }, | |
| 584 assertUnreachable | |
| 585 ); | |
| 586 | |
| 587 assertAsyncRan(); | |
| 588 assertAsyncRan(); | |
| 589 | |
| 590 resolve(1); | |
| 591 })(); | |
| 592 | |
| 593 // race/finally/then | |
| 594 (function() { | |
| 595 var resolve, reject; | |
| 596 var d1 = new Promise((x, y) => { | |
| 597 resolve = x; | |
| 598 reject = y; | |
| 599 }); | |
| 600 var d2 = new Promise((x, y) => { | |
| 601 resolve = x; | |
| 602 reject = y; | |
| 603 }); | |
| 604 | |
| 605 Promise.race([d1, d2]) | |
| 606 .finally(function onFinally() { | |
| 607 assertEqualsAsync(0, arguments.length); | |
| 608 }) | |
| 609 .then( | |
| 610 x => { | |
| 611 assertEqualsAsync(1, x); | |
| 612 }, | |
| 613 assertUnreachable | |
| 614 ); | |
| 615 | |
| 616 assertAsyncRan(); | |
| 617 assertAsyncRan(); | |
| 618 | |
| 619 resolve(1); | |
| 620 })(); | |
| 621 | |
| 622 var descriptor = Object.getOwnPropertyDescriptor(Promise.prototype, 'finally'); | |
| 623 assertTrue(descriptor.writable); | |
| 624 assertTrue(descriptor.configurable); | |
| 625 assertFalse(descriptor.enumerable); | |
| 626 | |
| 627 assertAsyncDone(); | |
| OLD | NEW |