disposal-during-throw-async-generator.js (1230B)
1 // |jit-test| skip-if: !getBuildConfiguration("explicit-resource-management"); --enable-explicit-resource-management 2 3 load(libdir + "asserts.js"); 4 5 const values = []; 6 7 async function* gen() { 8 using d = { 9 value: "d", 10 [Symbol.dispose]() { 11 values.push(this.value); 12 } 13 } 14 yield await Promise.resolve("a"); 15 yield await Promise.resolve("b"); 16 using c = { 17 value: "c", 18 [Symbol.dispose]() { 19 values.push(this.value); 20 } 21 } 22 throw new Error("err"); 23 } 24 25 async function testThrowInAsyncGenerator() { 26 let x = gen(); 27 values.push((await x.next()).value); 28 values.push((await x.next()).value); 29 await x.next().catch(() => {}); 30 } 31 testThrowInAsyncGenerator(); 32 drainJobQueue(); 33 assertArrayEq(values, ["a", "b", "c", "d"]); 34 35 const values2 = []; 36 async function* gen2() { 37 using c = { 38 value: "c", 39 [Symbol.dispose]() { 40 values2.push(this.value); 41 } 42 } 43 yield await Promise.resolve("a"); 44 yield await Promise.resolve("b"); 45 return; 46 } 47 48 async function testForcedThrowInAsyncGenerator() { 49 let x = gen2(); 50 values2.push((await x.next()).value); 51 await x.throw(new Error("err")).catch(() => {}); 52 } 53 testForcedThrowInAsyncGenerator(); 54 drainJobQueue(); 55 assertArrayEq(values2, ["a", "c"]);