1 // Copyright 2015 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: --allow-natives-syntax 6 7 // If a Promise's then method is overridden, that should be respected 8 // even if the promise is already resolved. x's resolution function is 9 // only called by Promise.resolve(); there shouldn't be a resolution 10 // check before when calling x.then. 11 12 13 // Async assert framework copied from mjsunit/es6/promises.js 14 15 var asyncAssertsExpected = 0; 16 17 function assertAsyncRan() { ++asyncAssertsExpected } 18 19 function assertLater(f, name) { 20 assertFalse(f()); // should not be true synchronously 21 ++asyncAssertsExpected; 22 var iterations = 0; 23 function runAssertion() { 24 if (f()) { 25 print(name, "succeeded"); 26 --asyncAssertsExpected; 27 } else if (iterations++ < 10) { 28 %EnqueueMicrotask(runAssertion); 29 } else { 30 %AbortJS(name + " FAILED!"); 31 } 32 } 33 %EnqueueMicrotask(runAssertion); 34 } 35 36 function assertAsyncDone(iteration) { 37 var iteration = iteration || 0; 38 %EnqueueMicrotask(function() { 39 if (asyncAssertsExpected === 0) 40 assertAsync(true, "all") 41 else if (iteration > 10) // Shouldn't take more. 42 assertAsync(false, "all... " + asyncAssertsExpected) 43 else 44 assertAsyncDone(iteration + 1) 45 }); 46 } 47 48 // End async assert framework 49 50 var y; 51 var x = Promise.resolve(); 52 x.then = () => { y = true; } 53 Promise.resolve().then(() => x); 54 assertLater(() => y === true, "y === true"); 55 56 assertAsyncDone(); 57