promise-resolution-order.html (2025B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Promise rejection ordering</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <link rel="help" href="https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint"> 7 <body> 8 <p>A microtask checkpoint should notify about rejected promises. After 9 <a 10 href="https://html.spec.whatwg.org/multipage/webappapis.html#clean-up-after-running-script">cleaning 11 up after running script</a> involves running a microtask checkpoint. So the order of unhandledrejection 12 should occur before error2. 13 </p> 14 15 <script> 16 'use strict'; 17 setup({ allow_uncaught_exception: true }); 18 19 async_test(function(t) { 20 const events = []; 21 addEventListener('unhandledrejection', t.step_func(() => { 22 events.push('unhandledrejection'); 23 })); 24 25 function insertInvalidScript(id) { 26 // Inserting <script> with an empty source schedules dispatching an 'error' event on 27 // the dom manipulation task source. 28 let script = document.createElement('script'); 29 script.setAttribute('src', ' '); 30 script.addEventListener('error', t.step_func(() => { 31 events.push(`error${id}`); 32 33 // This will be the end of the test. Verify the results are correct. 34 if (id == 2) { 35 assert_array_equals( 36 events, 37 [ 38 'raf1', 39 'resolve1', 40 'raf2', 41 'resolve2', 42 'error1', 43 'unhandledrejection', 44 'error2' 45 ] 46 ); 47 t.done(); 48 } 49 })); 50 document.body.append(script); 51 } 52 53 requestAnimationFrame(t.step_func(() => { 54 events.push('raf1'); 55 Promise.reject(); 56 Promise.resolve(0).then(t.step_func(() => { 57 events.push('resolve1'); 58 })); 59 insertInvalidScript(1); 60 })); 61 62 requestAnimationFrame(t.step_func(() => { 63 events.push('raf2'); 64 Promise.resolve(0).then(t.step_func(() => { 65 events.push('resolve2'); 66 })); 67 insertInvalidScript(2); 68 })); 69 }); 70 </script>