mapfn-result-awaited-once-per-iteration.js (1357B)
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 returned value from each invocation of the asynchronous mapping function 9 is awaited exactly once. 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 flags: [async] 16 includes: [asyncHelpers.js, compareArray.js, temporalHelpers.js] 17 features: [Array.fromAsync] 18 ---*/ 19 20 const calls = []; 21 const expected = [ 22 "call mapping", 23 "get thenable_0.then", 24 "call thenable_0.then", 25 "call mapping", 26 "get thenable_1.then", 27 "call thenable_1.then", 28 "call mapping", 29 "get thenable_2.then", 30 "call thenable_2.then", 31 ]; 32 33 function mapping(val, ix) { 34 calls.push("call mapping"); 35 const thenableName = `thenable_${ix}`; 36 return TemporalHelpers.propertyBagObserver(calls, { 37 then(resolve, reject) { 38 calls.push(`call ${thenableName}.then`); 39 resolve(val * 2); 40 } 41 }, thenableName) 42 } 43 44 asyncTest(async () => { 45 const result = await Array.fromAsync([1, 2, 3], mapping); 46 assert.compareArray(result, [2, 4, 6], "mapping function applied"); 47 assert.compareArray(calls, expected, "observable operations"); 48 });