mapfn-async-throws-close-sync-iterator.js (1181B)
1 // |reftest| async 2 // Copyright (C) 2023 Igalia, S.L. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 5 /*--- 6 esid: sec-array.fromasync 7 description: > 8 The iterator of a synchronous iterable is closed when the asynchronous mapping 9 function throws. 10 info: | 11 3.j.ii.6. If _mapping_ is *true*, then 12 a. Let _mappedValue_ be Call(_mapfn_, _thisArg_, « _nextValue_, 𝔽(_k_) »). 13 ... 14 c. Set _mappedValue_ to Await(_mappedValue_). 15 d. IfAbruptCloseAsyncIterator(_mappedValue_, _iteratorRecord_). 16 flags: [async] 17 includes: [asyncHelpers.js] 18 features: [Array.fromAsync] 19 ---*/ 20 21 let closed = false; 22 const iterator = { 23 next() { 24 return { value: 1, done: false }; 25 }, 26 return() { 27 closed = true; 28 return { done: true }; 29 }, 30 [Symbol.iterator]() { 31 return this; 32 } 33 } 34 35 asyncTest(async () => { 36 await assert.throwsAsync(Error, () => Array.fromAsync(iterator, async (val) => { 37 assert.sameValue(val, 1, "mapfn receives value from iterator"); 38 throw new Error("mapfn throws"); 39 }), "async mapfn rejecting should cause fromAsync to reject"); 40 assert(closed, "async mapfn rejecting should close iterator") 41 });