ticks-with-sync-iter-resolved-promise-and-constructor-lookup.js (2394B)
1 // |reftest| async 2 // Copyright (C) 2019 André Bargull. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 5 /*--- 6 esid: sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset 7 description: > 8 Ensure the number of ticks and Promise constructor lookups is correct with a Async-from-Sync iterator. 9 info: | 10 13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind, 11 lhsKind, labelSet [ , iteratorKind ] ) 12 25.1.4.2.1 %AsyncFromSyncIteratorPrototype%.next 13 25.1.4.4 AsyncFromSyncIteratorContinuation 14 25.6.4.5.1 PromiseResolve 15 6.2.3.1 Await 16 17 includes: [compareArray.js] 18 flags: [async] 19 features: [async-iteration] 20 ---*/ 21 22 // The expected event log. 23 var expected = [ 24 // Before entering loop. 25 "pre", 26 27 // %AsyncFromSyncIteratorPrototype%.next 28 // -> AsyncFromSyncIteratorContinuation 29 // -> PromiseResolve 30 "constructor", 31 32 // Await 33 // -> PromiseResolve 34 "constructor", 35 36 // Async-from-Sync Iterator promise resolved. 37 "tick 1", 38 39 // Await promise resolved. 40 "tick 2", 41 42 // In loop body. 43 "loop", 44 45 // Await 46 // -> PromiseResolve 47 "constructor", 48 49 // Async-from-Sync Iterator promise resolved. 50 "tick 3", 51 52 // Await promise resolved 53 "tick 4", 54 55 // After exiting loop. 56 "post", 57 ]; 58 59 // The actual event log. 60 var actual = []; 61 62 // Test function using for-await with a single, already resolved Promise. 63 async function f() { 64 var p = Promise.resolve(0); 65 actual.push("pre"); 66 for await (var x of [p]) { 67 actual.push("loop"); 68 } 69 actual.push("post"); 70 } 71 72 // Count the number of ticks needed to complete the loop and compare the actual log. 73 Promise.resolve(0) 74 .then(() => actual.push("tick 1")) 75 .then(() => actual.push("tick 2")) 76 .then(() => actual.push("tick 3")) 77 .then(() => actual.push("tick 4")) 78 .then(() => { 79 assert.compareArray(actual, expected, "Ticks and constructor lookups"); 80 }).then($DONE, $DONE); 81 82 // Redefine `Promise.constructor` in order to intercept "constructor" lookups from PromiseResolve. 83 // (Perform last so that the lookups from SpeciesConstructor in `then` aren't logged.) 84 Object.defineProperty(Promise.prototype, "constructor", { 85 get() { 86 actual.push("constructor"); 87 return Promise; 88 }, 89 configurable: true, 90 }); 91 92 // Start the asynchronous function. 93 f();