Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | // Flags: --allow-natives-syntax |
| 29 | |
| 30 | var ordering = []; |
| 31 | function reset() { |
| 32 | ordering = []; |
| 33 | } |
| 34 | |
| 35 | function assertArrayValues(expected, actual) { |
| 36 | assertEquals(expected.length, actual.length); |
| 37 | for (var i = 0; i < expected.length; i++) { |
| 38 | assertEquals(expected[i], actual[i]); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | function assertOrdering(expected) { |
| 43 | %RunMicrotasks(); |
| 44 | assertArrayValues(expected, ordering); |
| 45 | } |
| 46 | |
| 47 | function newPromise(id, fn) { |
| 48 | var r; |
| 49 | var t = 1; |
| 50 | var promise = new Promise(function(resolve) { |
| 51 | r = resolve; |
| 52 | if (fn) fn(); |
| 53 | }); |
| 54 | |
| 55 | var next = promise.then(function(value) { |
| 56 | ordering.push('p' + id); |
| 57 | return value; |
| 58 | }); |
| 59 | |
| 60 | return { |
| 61 | resolve: r, |
| 62 | then: function(fn) { |
| 63 | next = next.then(function(value) { |
| 64 | ordering.push('p' + id + ':' + t++); |
| 65 | return fn ? fn(value) : value; |
| 66 | }); |
| 67 | |
| 68 | return this; |
| 69 | } |
| 70 | }; |
| 71 | } |
| 72 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | (function PromiseThens() { |
| 74 | reset(); |
| 75 | |
| 76 | var p1 = newPromise(1).then(); |
| 77 | var p2 = newPromise(2).then(); |
| 78 | |
| 79 | p1.resolve(); |
| 80 | p2.resolve(); |
| 81 | |
| 82 | assertOrdering(['p1', 'p2', 'p1:1', 'p2:1']); |
| 83 | })(); |