microtask_after_script.html (1507B)
1 <!DOCTYPE html> 2 <head> 3 <link rel=author title="Aleks Totic" href="mailto:atotic@chromium.org"> 4 <link rel=help href="https://html.spec.whatwg.org/#clean-up-after-running-script"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="resources/common.js"></script> 8 </head> 9 <body style="height:2000px;"> 10 <script> 11 /* 12 promise 1, promise 2 execute immediately after script tag 13 promise 1 child executes immediately after promise 2. 14 15 Relevant specs: 16 17 https://html.spec.whatwg.org/#clean-up-after-running-script 18 If the JavaScript execution context stack is now empty, perform a microtask checkpoint. 19 20 https://html.spec.whatwg.org/#perform-a-microtask-checkpoint 21 "perform a microtask checkpoint" runs in a loop until all microtasks have been delivered. 22 */ 23 24 var test = async_test("Microtask immediately after script"); 25 26 var events = []; 27 28 Promise.resolve() 29 .then(function() { 30 events.push("promise 1"); 31 return Promise.resolve(); 32 }) 33 .then(function() { 34 test.step(function() { 35 events.push("promise 1 child"); 36 assert_array_equals(events, ["promise 1", "promise 2", "promise 1 child"]); 37 test.done(); 38 }); 39 }); 40 Promise.resolve() 41 .then(function() { 42 events.push("promise 2"); 43 }); 44 45 // Set up events that must be executed after Promise. 46 window.setTimeout(function() { 47 events.push('timeout'); 48 }, 0); 49 window.addEventListener('scroll', function() { 50 events.push('scroll'); 51 }); 52 window.scrollBy(0,10); 53 54 </script> 55 </body>