Home | History | Annotate | Download | only in harmony
      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   assertThrows(function() { new Promise(5) }, TypeError)
     62 })();
     63 
     64 (function() {
     65   assertThrows(function() { new Promise(function() { throw 5 }) }, 5)
     66 })();
     67 
     68 (function() {
     69   Promise.resolved(5);
     70   Promise.resolved(5).chain(undefined, assertUnreachable).chain(
     71     function(x) { assertAsync(x === 5, "resolved/chain-nohandler") },
     72     assertUnreachable
     73   )
     74   assertAsyncRan()
     75 })();
     76 
     77 (function() {
     78   Promise.rejected(5).chain(assertUnreachable, undefined).chain(
     79     assertUnreachable,
     80     function(r) { assertAsync(r === 5, "rejected/chain-nohandler") }
     81   )
     82   assertAsyncRan()
     83 })();
     84 
     85 (function() {
     86   Promise.resolved(5).then(undefined, assertUnreachable).chain(
     87     function(x) { assertAsync(x === 5, "resolved/then-nohandler") },
     88     assertUnreachable
     89   )
     90   assertAsyncRan()
     91 })();
     92 
     93 (function() {
     94   Promise.rejected(5).then(assertUnreachable, undefined).chain(
     95     assertUnreachable,
     96     function(r) { assertAsync(r === 5, "rejected/then-nohandler") }
     97   )
     98   assertAsyncRan()
     99 })();
    100 
    101 (function() {
    102   var p1 = Promise.resolved(5)
    103   var p2 = Promise.resolved(p1)
    104   var p3 = Promise.resolved(p2)
    105   p3.chain(
    106     function(x) { assertAsync(x === p2, "resolved/chain") },
    107     assertUnreachable
    108   )
    109   assertAsyncRan()
    110 })();
    111 
    112 (function() {
    113   var p1 = Promise.resolved(5)
    114   var p2 = Promise.resolved(p1)
    115   var p3 = Promise.resolved(p2)
    116   p3.then(
    117     function(x) { assertAsync(x === 5, "resolved/then") },
    118     assertUnreachable
    119   )
    120   assertAsyncRan()
    121 })();
    122 
    123 (function() {
    124   var p1 = Promise.rejected(5)
    125   var p2 = Promise.resolved(p1)
    126   var p3 = Promise.resolved(p2)
    127   p3.chain(
    128     function(x) { assertAsync(x === p2, "rejected/chain") },
    129     assertUnreachable
    130   )
    131   assertAsyncRan()
    132 })();
    133 
    134 (function() {
    135   var p1 = Promise.rejected(5)
    136   var p2 = Promise.resolved(p1)
    137   var p3 = Promise.resolved(p2)
    138   p3.then(
    139     assertUnreachable,
    140     function(x) { assertAsync(x === 5, "rejected/then") }
    141   )
    142   assertAsyncRan()
    143 })();
    144 
    145 (function() {
    146   var p1 = Promise.resolved(5)
    147   var p2 = Promise.resolved(p1)
    148   var p3 = Promise.resolved(p2)
    149   p3.chain(function(x) { return x }, assertUnreachable).chain(
    150     function(x) { assertAsync(x === p1, "resolved/chain/chain") },
    151     assertUnreachable
    152   )
    153   assertAsyncRan()
    154 })();
    155 
    156 (function() {
    157   var p1 = Promise.resolved(5)
    158   var p2 = Promise.resolved(p1)
    159   var p3 = Promise.resolved(p2)
    160   p3.chain(function(x) { return x }, assertUnreachable).then(
    161     function(x) { assertAsync(x === 5, "resolved/chain/then") },
    162     assertUnreachable
    163   )
    164   assertAsyncRan()
    165 })();
    166 
    167 (function() {
    168   var p1 = Promise.resolved(5)
    169   var p2 = Promise.resolved(p1)
    170   var p3 = Promise.resolved(p2)
    171   p3.chain(function(x) { return 6 }, assertUnreachable).chain(
    172     function(x) { assertAsync(x === 6, "resolved/chain/chain2") },
    173     assertUnreachable
    174   )
    175   assertAsyncRan()
    176 })();
    177 
    178 (function() {
    179   var p1 = Promise.resolved(5)
    180   var p2 = Promise.resolved(p1)
    181   var p3 = Promise.resolved(p2)
    182   p3.chain(function(x) { return 6 }, assertUnreachable).then(
    183     function(x) { assertAsync(x === 6, "resolved/chain/then2") },
    184     assertUnreachable
    185   )
    186   assertAsyncRan()
    187 })();
    188 
    189 (function() {
    190   var p1 = Promise.resolved(5)
    191   var p2 = Promise.resolved(p1)
    192   var p3 = Promise.resolved(p2)
    193   p3.then(function(x) { return x + 1 }, assertUnreachable).chain(
    194     function(x) { assertAsync(x === 6, "resolved/then/chain") },
    195     assertUnreachable
    196   )
    197   assertAsyncRan()
    198 })();
    199 
    200 (function() {
    201   var p1 = Promise.resolved(5)
    202   var p2 = Promise.resolved(p1)
    203   var p3 = Promise.resolved(p2)
    204   p3.then(function(x) { return x + 1 }, assertUnreachable).then(
    205     function(x) { assertAsync(x === 6, "resolved/then/then") },
    206     assertUnreachable
    207   )
    208   assertAsyncRan()
    209 })();
    210 
    211 (function() {
    212   var p1 = Promise.resolved(5)
    213   var p2 = Promise.resolved(p1)
    214   var p3 = Promise.resolved(p2)
    215   p3.then(function(x){ return Promise.resolved(x+1) }, assertUnreachable).chain(
    216     function(x) { assertAsync(x === 6, "resolved/then/chain2") },
    217     assertUnreachable
    218   )
    219   assertAsyncRan()
    220 })();
    221 
    222 (function() {
    223   var p1 = Promise.resolved(5)
    224   var p2 = Promise.resolved(p1)
    225   var p3 = Promise.resolved(p2)
    226   p3.then(function(x) { return Promise.resolved(x+1) }, assertUnreachable).then(
    227     function(x) { assertAsync(x === 6, "resolved/then/then2") },
    228     assertUnreachable
    229   )
    230   assertAsyncRan()
    231 })();
    232 
    233 (function() {
    234   var p1 = Promise.resolved(5)
    235   var p2 = Promise.resolved(p1)
    236   var p3 = Promise.resolved(p2)
    237   p3.chain(function(x) { throw 6 }, assertUnreachable).chain(
    238     assertUnreachable,
    239     function(x) { assertAsync(x === 6, "resolved/chain-throw/chain") }
    240   )
    241   assertAsyncRan()
    242 })();
    243 
    244 (function() {
    245   var p1 = Promise.resolved(5)
    246   var p2 = Promise.resolved(p1)
    247   var p3 = Promise.resolved(p2)
    248   p3.chain(function(x) { throw 6 }, assertUnreachable).then(
    249     assertUnreachable,
    250     function(x) { assertAsync(x === 6, "resolved/chain-throw/then") }
    251   )
    252   assertAsyncRan()
    253 })();
    254 
    255 (function() {
    256   var p1 = Promise.resolved(5)
    257   var p2 = Promise.resolved(p1)
    258   var p3 = Promise.resolved(p2)
    259   p3.then(function(x) { throw 6 }, assertUnreachable).chain(
    260     assertUnreachable,
    261     function(x) { assertAsync(x === 6, "resolved/then-throw/chain") }
    262   )
    263   assertAsyncRan()
    264 })();
    265 
    266 (function() {
    267   var p1 = Promise.resolved(5)
    268   var p2 = Promise.resolved(p1)
    269   var p3 = Promise.resolved(p2)
    270   p3.then(function(x) { throw 6 }, assertUnreachable).then(
    271     assertUnreachable,
    272     function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
    273   )
    274   assertAsyncRan()
    275 })();
    276 
    277 (function() {
    278   var p1 = Promise.resolved(5)
    279   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    280   var p3 = Promise.resolved(p2)
    281   p3.chain(
    282     function(x) { assertAsync(x === p2, "resolved/thenable/chain") },
    283     assertUnreachable
    284   )
    285   assertAsyncRan()
    286 })();
    287 
    288 (function() {
    289   var p1 = Promise.resolved(5)
    290   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    291   var p3 = Promise.resolved(p2)
    292   p3.then(
    293     function(x) { assertAsync(x === 5, "resolved/thenable/then") },
    294     assertUnreachable
    295   )
    296   assertAsyncRan()
    297 })();
    298 
    299 (function() {
    300   var p1 = Promise.rejected(5)
    301   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    302   var p3 = Promise.resolved(p2)
    303   p3.chain(
    304     function(x) { assertAsync(x === p2, "rejected/thenable/chain") },
    305     assertUnreachable
    306   )
    307   assertAsyncRan()
    308 })();
    309 
    310 (function() {
    311   var p1 = Promise.rejected(5)
    312   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    313   var p3 = Promise.resolved(p2)
    314   p3.then(
    315     assertUnreachable,
    316     function(x) { assertAsync(x === 5, "rejected/thenable/then") }
    317   )
    318   assertAsyncRan()
    319 })();
    320 
    321 (function() {
    322   var deferred = Promise.deferred()
    323   var p1 = deferred.promise
    324   var p2 = Promise.resolved(p1)
    325   var p3 = Promise.resolved(p2)
    326   p3.chain(
    327     function(x) { assertAsync(x === p2, "chain/resolve") },
    328     assertUnreachable
    329   )
    330   deferred.resolve(5)
    331   assertAsyncRan()
    332 })();
    333 
    334 (function() {
    335   var deferred = Promise.deferred()
    336   var p1 = deferred.promise
    337   var p2 = Promise.resolved(p1)
    338   var p3 = Promise.resolved(p2)
    339   p3.then(
    340     function(x) { assertAsync(x === 5, "then/resolve") },
    341     assertUnreachable
    342   )
    343   deferred.resolve(5)
    344   assertAsyncRan()
    345 })();
    346 
    347 (function() {
    348   var deferred = Promise.deferred()
    349   var p1 = deferred.promise
    350   var p2 = Promise.resolved(p1)
    351   var p3 = Promise.resolved(p2)
    352   p3.chain(
    353     function(x) { assertAsync(x === p2, "chain/reject") },
    354     assertUnreachable
    355   )
    356   deferred.reject(5)
    357   assertAsyncRan()
    358 })();
    359 
    360 (function() {
    361   var deferred = Promise.deferred()
    362   var p1 = deferred.promise
    363   var p2 = Promise.resolved(p1)
    364   var p3 = Promise.resolved(p2)
    365   p3.then(
    366     assertUnreachable,
    367     function(x) { assertAsync(x === 5, "then/reject") }
    368   )
    369   deferred.reject(5)
    370   assertAsyncRan()
    371 })();
    372 
    373 (function() {
    374   var deferred = Promise.deferred()
    375   var p1 = deferred.promise
    376   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    377   var p3 = Promise.resolved(p2)
    378   p3.chain(
    379     function(x) { assertAsync(x === p2, "chain/resolve/thenable") },
    380     assertUnreachable
    381   )
    382   deferred.resolve(5)
    383   assertAsyncRan()
    384 })();
    385 
    386 (function() {
    387   var deferred = Promise.deferred()
    388   var p1 = deferred.promise
    389   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    390   var p3 = Promise.resolved(p2)
    391   p3.then(
    392     function(x) { assertAsync(x === 5, "then/resolve/thenable") },
    393     assertUnreachable
    394   )
    395   deferred.resolve(5)
    396   assertAsyncRan()
    397 })();
    398 
    399 (function() {
    400   var deferred = Promise.deferred()
    401   var p1 = deferred.promise
    402   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    403   var p3 = Promise.resolved(p2)
    404   p3.chain(
    405     function(x) { assertAsync(x === p2, "chain/reject/thenable") },
    406     assertUnreachable
    407   )
    408   deferred.reject(5)
    409   assertAsyncRan()
    410 })();
    411 
    412 (function() {
    413   var deferred = Promise.deferred()
    414   var p1 = deferred.promise
    415   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    416   var p3 = Promise.resolved(p2)
    417   p3.then(
    418     assertUnreachable,
    419     function(x) { assertAsync(x === 5, "then/reject/thenable") }
    420   )
    421   deferred.reject(5)
    422   assertAsyncRan()
    423 })();
    424 
    425 (function() {
    426   var p1 = Promise.resolved(5)
    427   var p2 = Promise.resolved(p1)
    428   var deferred = Promise.deferred()
    429   var p3 = deferred.promise
    430   p3.chain(
    431     function(x) { assertAsync(x === p2, "chain/resolve2") },
    432     assertUnreachable
    433   )
    434   deferred.resolve(p2)
    435   assertAsyncRan()
    436 })();
    437 
    438 (function() {
    439   var p1 = Promise.resolved(5)
    440   var p2 = Promise.resolved(p1)
    441   var deferred = Promise.deferred()
    442   var p3 = deferred.promise
    443   p3.then(
    444     function(x) { assertAsync(x === 5, "then/resolve2") },
    445     assertUnreachable
    446   )
    447   deferred.resolve(p2)
    448   assertAsyncRan()
    449 })();
    450 
    451 (function() {
    452   var p1 = Promise.resolved(5)
    453   var p2 = Promise.resolved(p1)
    454   var deferred = Promise.deferred()
    455   var p3 = deferred.promise
    456   p3.chain(
    457     assertUnreachable,
    458     function(x) { assertAsync(x === 5, "chain/reject2") }
    459   )
    460   deferred.reject(5)
    461   assertAsyncRan()
    462 })();
    463 
    464 (function() {
    465   var p1 = Promise.resolved(5)
    466   var p2 = Promise.resolved(p1)
    467   var deferred = Promise.deferred()
    468   var p3 = deferred.promise
    469   p3.then(
    470     assertUnreachable,
    471     function(x) { assertAsync(x === 5, "then/reject2") }
    472   )
    473   deferred.reject(5)
    474   assertAsyncRan()
    475 })();
    476 
    477 (function() {
    478   var p1 = Promise.resolved(5)
    479   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    480   var deferred = Promise.deferred()
    481   var p3 = deferred.promise
    482   p3.chain(
    483     function(x) { assertAsync(x === p2, "chain/resolve/thenable2") },
    484     assertUnreachable
    485   )
    486   deferred.resolve(p2)
    487   assertAsyncRan()
    488 })();
    489 
    490 (function() {
    491   var p1 = Promise.resolved(5)
    492   var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
    493   var deferred = Promise.deferred()
    494   var p3 = deferred.promise
    495   p3.then(
    496     function(x) { assertAsync(x === 5, "then/resolve/thenable2") },
    497     assertUnreachable
    498   )
    499   deferred.resolve(p2)
    500   assertAsyncRan()
    501 })();
    502 
    503 (function() {
    504   var p1 = Promise.resolved(0)
    505   var p2 = p1.chain(function(x) { return p2 }, assertUnreachable)
    506   p2.chain(
    507     assertUnreachable,
    508     function(r) { assertAsync(r instanceof TypeError, "cyclic/chain") }
    509   )
    510   assertAsyncRan()
    511 })();
    512 
    513 (function() {
    514   var p1 = Promise.resolved(0)
    515   var p2 = p1.then(function(x) { return p2 }, assertUnreachable)
    516   p2.chain(
    517     assertUnreachable,
    518     function(r) { assertAsync(r instanceof TypeError, "cyclic/then") }
    519   )
    520   assertAsyncRan()
    521 })();
    522 
    523 (function() {
    524   var deferred = Promise.deferred()
    525   var p = deferred.promise
    526   deferred.resolve(p)
    527   p.chain(
    528     function(x) { assertAsync(x === p, "cyclic/deferred/chain") },
    529     assertUnreachable
    530   )
    531   assertAsyncRan()
    532 })();
    533 
    534 (function() {
    535   var deferred = Promise.deferred()
    536   var p = deferred.promise
    537   deferred.resolve(p)
    538   p.then(
    539     assertUnreachable,
    540     function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then") }
    541   )
    542   assertAsyncRan()
    543 })();
    544 
    545 (function() {
    546   Promise.all([]).chain(
    547     function(x) { assertAsync(x.length === 0, "all/resolve/empty") },
    548     assertUnreachable
    549   )
    550   assertAsyncRan()
    551 })();
    552 
    553 (function() {
    554   var deferred1 = Promise.deferred()
    555   var p1 = deferred1.promise
    556   var deferred2 = Promise.deferred()
    557   var p2 = deferred2.promise
    558   var deferred3 = Promise.deferred()
    559   var p3 = deferred3.promise
    560   Promise.all([p1, p2, p3]).chain(
    561     function(x) {
    562       assertAsync(x.length === 3, "all/resolve")
    563       assertAsync(x[0] === 1, "all/resolve/0")
    564       assertAsync(x[1] === 2, "all/resolve/1")
    565       assertAsync(x[2] === 3, "all/resolve/2")
    566     },
    567     assertUnreachable
    568   )
    569   deferred1.resolve(1)
    570   deferred3.resolve(3)
    571   deferred2.resolve(2)
    572   assertAsyncRan()
    573   assertAsyncRan()
    574   assertAsyncRan()
    575   assertAsyncRan()
    576 })();
    577 
    578 (function() {
    579   var deferred = Promise.deferred()
    580   var p1 = deferred.promise
    581   var p2 = Promise.resolved(2)
    582   var p3 = Promise.deferred().promise
    583   Promise.all([p1, p2, p3]).chain(
    584     assertUnreachable,
    585     assertUnreachable
    586   )
    587   deferred.resolve(1)
    588 })();
    589 
    590 (function() {
    591   var deferred1 = Promise.deferred()
    592   var p1 = deferred1.promise
    593   var deferred2 = Promise.deferred()
    594   var p2 = deferred2.promise
    595   var deferred3 = Promise.deferred()
    596   var p3 = deferred3.promise
    597   Promise.all([p1, p2, p3]).chain(
    598     assertUnreachable,
    599     function(x) { assertAsync(x === 2, "all/reject") }
    600   )
    601   deferred1.resolve(1)
    602   deferred3.resolve(3)
    603   deferred2.reject(2)
    604   assertAsyncRan()
    605 })();
    606 
    607 (function() {
    608   Promise.one([]).chain(
    609     assertUnreachable,
    610     assertUnreachable
    611   )
    612 })();
    613 
    614 (function() {
    615   var p1 = Promise.resolved(1)
    616   var p2 = Promise.resolved(2)
    617   var p3 = Promise.resolved(3)
    618   Promise.one([p1, p2, p3]).chain(
    619     function(x) { assertAsync(x === 1, "resolved/all") },
    620     assertUnreachable
    621   )
    622   assertAsyncRan()
    623 })();
    624 
    625 (function() {
    626   var p1 = Promise.resolved(1)
    627   var p2 = Promise.resolved(2)
    628   var p3 = Promise.resolved(3)
    629   Promise.one([0, p1, p2, p3]).chain(
    630     function(x) { assertAsync(x === 0, "resolved-const/all") },
    631     assertUnreachable
    632   )
    633   assertAsyncRan()
    634 })();
    635 
    636 (function() {
    637   var deferred1 = Promise.deferred()
    638   var p1 = deferred1.promise
    639   var deferred2 = Promise.deferred()
    640   var p2 = deferred2.promise
    641   var deferred3 = Promise.deferred()
    642   var p3 = deferred3.promise
    643   Promise.one([p1, p2, p3]).chain(
    644     function(x) { assertAsync(x === 3, "one/resolve") },
    645     assertUnreachable
    646   )
    647   deferred3.resolve(3)
    648   deferred1.resolve(1)
    649   assertAsyncRan()
    650 })();
    651 
    652 (function() {
    653   var deferred = Promise.deferred()
    654   var p1 = deferred.promise
    655   var p2 = Promise.resolved(2)
    656   var p3 = Promise.deferred().promise
    657   Promise.one([p1, p2, p3]).chain(
    658     function(x) { assertAsync(x === 2, "resolved/one") },
    659     assertUnreachable
    660   )
    661   deferred.resolve(1)
    662   assertAsyncRan()
    663 })();
    664 
    665 (function() {
    666   var deferred1 = Promise.deferred()
    667   var p1 = deferred1.promise
    668   var deferred2 = Promise.deferred()
    669   var p2 = deferred2.promise
    670   var deferred3 = Promise.deferred()
    671   var p3 = deferred3.promise
    672   Promise.one([p1, p2, p3]).chain(
    673     function(x) { assertAsync(x === 3, "one/resolve/reject") },
    674     assertUnreachable
    675   )
    676   deferred3.resolve(3)
    677   deferred1.reject(1)
    678   assertAsyncRan()
    679 })();
    680 
    681 (function() {
    682   var deferred1 = Promise.deferred()
    683   var p1 = deferred1.promise
    684   var deferred2 = Promise.deferred()
    685   var p2 = deferred2.promise
    686   var deferred3 = Promise.deferred()
    687   var p3 = deferred3.promise
    688   Promise.one([p1, p2, p3]).chain(
    689     assertUnreachable,
    690     function(x) { assertAsync(x === 3, "one/reject/resolve") }
    691   )
    692   deferred3.reject(3)
    693   deferred1.resolve(1)
    694   assertAsyncRan()
    695 })();
    696 
    697 (function() {
    698   var log
    699   function MyPromise(resolver) {
    700     log += "n"
    701     Promise.call(this,
    702       function(resolve, reject) {
    703         resolver(
    704           function(x) { log += "x" + x; resolve(x) },
    705           function(r) { log += "r" + r; reject(r) }
    706         )
    707       }
    708     )
    709   }
    710 
    711   MyPromise.__proto__ = Promise
    712   MyPromise.deferred = function() {
    713     log += "d"
    714     return this.__proto__.deferred.call(this)
    715   }
    716 
    717   MyPromise.prototype.__proto__ = Promise.prototype
    718   MyPromise.prototype.chain = function(resolve, reject) {
    719     log += "w"
    720     return this.__proto__.__proto__.chain.call(this, resolve, reject)
    721   }
    722 
    723   log = ""
    724   var p1 = new MyPromise(function(resolve, reject) { resolve(1) })
    725   var p2 = new MyPromise(function(resolve, reject) { reject(2) })
    726   var d3 = MyPromise.deferred()
    727   assertTrue(d3.promise instanceof MyPromise, "subclass/instance3")
    728   assertTrue(log === "nx1nr2dn", "subclass/create")
    729 
    730   log = ""
    731   var p4 = MyPromise.resolved(4)
    732   var p5 = MyPromise.rejected(5)
    733   assertTrue(p4 instanceof MyPromise, "subclass/instance4")
    734   assertTrue(p5 instanceof MyPromise, "subclass/instance5")
    735   d3.resolve(3)
    736   assertTrue(log === "nx4nr5x3", "subclass/resolve")
    737 
    738   log = ""
    739   var d6 = MyPromise.deferred()
    740   d6.promise.chain(function(x) { return new Promise(x) }).chain(function() {})
    741   d6.resolve(6)
    742   assertTrue(log === "dnwnwnx6", "subclass/chain")
    743 
    744   log = ""
    745   Promise.all([11, Promise.resolved(12), 13, MyPromise.resolved(14), 15, 16])
    746   assertTrue(log === "nx14wn", "subclass/all/arg")
    747 
    748   log = ""
    749   MyPromise.all([21, Promise.resolved(22), 23, MyPromise.resolved(24), 25, 26])
    750   assertTrue(log === "nx24dnnx21wnnx23wnwnnx25wnnx26wn", "subclass/all/self")
    751 })();
    752 
    753 
    754 assertAsyncDone()
    755