response-body-read-task-handling.html (3202B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 </head> 9 <body> 10 <script> 11 function performMicrotaskCheckpoint() { 12 document.createNodeIterator(document, -1, { 13 acceptNode() { 14 return NodeFilter.FILTER_ACCEPT; 15 } 16 }).nextNode(); 17 } 18 19 promise_test(function() { 20 return fetch("../resources/data.json").then(function(response) { 21 // Add a getter for "then" that will incidentally be invoked 22 // during promise resolution. 23 Object.prototype.__defineGetter__('then', () => { 24 // Clean up behind ourselves. 25 delete Object.prototype.then; 26 27 // This promise should (like all promises) be resolved 28 // asynchronously. 29 var executed = false; 30 Promise.resolve().then(_ => { executed = true; }); 31 32 // This shouldn't run microtasks! They should only run 33 // after the fetch is resolved. 34 performMicrotaskCheckpoint(); 35 36 // The fulfill handler above shouldn't have run yet. If it has run, 37 // throw to reject this promise and fail the test. 38 assert_false(executed, "shouldn't have run microtasks yet"); 39 40 // Otherwise act as if there's no "then" property so the promise 41 // fulfills and the test passes. 42 return undefined; 43 }); 44 45 // Create a read request, incidentally resolving a promise with an 46 // object value, thereby invoking the getter installed above. 47 return response.body.getReader().read(); 48 }); 49 }, "reading from a body stream should occur in a microtask scope"); 50 51 promise_test(function() { 52 return fetch("../resources/data.json").then(function(response) { 53 // Add a getter for "then" that will incidentally be invoked 54 // during promise resolution. 55 Object.prototype.__defineGetter__('then', () => { 56 // Clean up behind ourselves. 57 delete Object.prototype.then; 58 59 // This promise should (like all promises) be resolved 60 // asynchronously. 61 var executed = false; 62 Promise.resolve().then(_ => { executed = true; }); 63 64 // This shouldn't run microtasks! They should only run 65 // after the fetch is resolved. 66 performMicrotaskCheckpoint(); 67 68 // The fulfill handler above shouldn't have run yet. If it has run, 69 // throw to reject this promise and fail the test. 70 assert_false(executed, "shouldn't have run microtasks yet"); 71 72 // Otherwise act as if there's no "then" property so the promise 73 // fulfills and the test passes. 74 return undefined; 75 }); 76 77 // Create a read request, incidentally resolving a promise with an 78 // object value, thereby invoking the getter installed above. 79 return response.body.pipeTo(new WritableStream({ 80 write(chunk) {} 81 })) 82 }); 83 }, "piping from a body stream to a JS-written WritableStream should occur in a microtask scope"); 84 </script> 85 </body> 86 </html>