Skip to content

Commit b4c45be

Browse files
committed
Normative: Add Array.fromAsync
From [proposal-array-from-async](https://github.com/tc39/proposal-array-from-async).
1 parent a545cb8 commit b4c45be

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

spec.html

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7393,6 +7393,20 @@ <h1>
73937393
</emu-alg>
73947394
</emu-clause>
73957395

7396+
<emu-clause id="sec-ifabruptcloseasynciterator" aoid="IfAbruptCloseAsyncIterator">
7397+
<h1>IfAbruptCloseAsyncIterator ( _value_, _iteratorRecord_ )</h1>
7398+
<p>IfAbruptCloseAsyncIterator is a shorthand for a sequence of algorithm steps that use an Iterator Record. An algorithm step of the form:</p>
7399+
<emu-alg>
7400+
1. IfAbruptCloseAsyncIterator(_value_, _iteratorRecord_).
7401+
</emu-alg>
7402+
<p>means the same thing as:</p>
7403+
<emu-alg>
7404+
1. Assert: _value_ is a Completion Record.
7405+
1. If _value_ is an abrupt completion, return ? AsyncIteratorClose(_iteratorRecord_, _value_).
7406+
1. Else, set _value_ to ! _value_.
7407+
</emu-alg>
7408+
</emu-clause>
7409+
73967410
<emu-clause id="sec-createiterresultobject" type="abstract operation">
73977411
<h1>
73987412
CreateIteratorResultObject (
@@ -39843,6 +39857,82 @@ <h1>Array.from ( _items_ [ , _mapper_ [ , _thisArg_ ] ] )</h1>
3984339857
</emu-note>
3984439858
</emu-clause>
3984539859

39860+
<emu-clause id="sec-array.fromAsync">
39861+
<h1>Array.fromAsync ( _asyncItems_ [ , _mapper_ [ , _thisArg_ ] ] )</h1>
39862+
39863+
<p>This async method performs the following steps when called:</p>
39864+
<emu-alg>
39865+
1. Let _C_ be the *this* value.
39866+
1. If _mapper_ is *undefined*, then
39867+
1. Let _mapping_ be *false*.
39868+
1. Else,
39869+
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
39870+
1. Let _mapping_ be *true*.
39871+
1. Let _usingAsyncIterator_ be ? GetMethod(_asyncItems_, %Symbol.asyncIterator%).
39872+
1. If _usingAsyncIterator_ is *undefined*, then
39873+
1. Let _usingSyncIterator_ be ? GetMethod(_asyncItems_, %Symbol.iterator%).
39874+
1. Let _iteratorRecord_ be *undefined*.
39875+
1. If _usingAsyncIterator_ is not *undefined*, then
39876+
1. Set _iteratorRecord_ to ? GetIteratorFromMethod(_asyncItems_, _usingAsyncIterator_).
39877+
1. Else if _usingSyncIterator_ is not *undefined*, then
39878+
1. Set _iteratorRecord_ to CreateAsyncFromSyncIterator(? GetIteratorFromMethod(_asyncItems_, _usingSyncIterator_)).
39879+
1. If _iteratorRecord_ is not *undefined*, then
39880+
1. If IsConstructor(_C_) is *true*, then
39881+
1. Let _A_ be ? Construct(_C_).
39882+
1. Else,
39883+
1. Let _A_ be ! ArrayCreate(0).
39884+
1. Let _k_ be 0.
39885+
1. Repeat,
39886+
1. If _k_ ≥ 2<sup>53</sup> - 1, then
39887+
1. Let _error_ be ThrowCompletion(a newly created *TypeError* object).
39888+
1. Return ? AsyncIteratorClose(_iteratorRecord_, _error_).
39889+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
39890+
1. Let _nextResult_ be ? Call(_iteratorRecord_.[[NextMethod]], _iteratorRecord_.[[Iterator]]).
39891+
1. Set _nextResult_ to ? Await(_nextResult_).
39892+
1. If _nextResult_ is not an Object, throw a *TypeError* exception.
39893+
1. Let _done_ be ? IteratorComplete(_nextResult_).
39894+
1. If _done_ is *true*, then
39895+
1. Perform ? Set(_A_, *"length"*, 𝔽(_k_), *true*).
39896+
1. Return _A_.
39897+
1. Let _nextValue_ be ? IteratorValue(_nextResult_).
39898+
1. If _mapping_ is *true*, then
39899+
1. Let _mappedValue_ be Completion(Call(_mapper_, _thisArg_, « _nextValue_, 𝔽(_k_) »)).
39900+
1. IfAbruptCloseAsyncIterator(_mappedValue_, _iteratorRecord_).
39901+
1. Set _mappedValue_ to Completion(Await(_mappedValue_)).
39902+
1. IfAbruptCloseAsyncIterator(_mappedValue_, _iteratorRecord_).
39903+
1. Else,
39904+
1. Let _mappedValue_ be _nextValue_.
39905+
1. Let _defineStatus_ be Completion(CreateDataPropertyOrThrow(_A_, _Pk_, _mappedValue_)).
39906+
1. IfAbruptCloseAsyncIterator(_defineStatus_, _iteratorRecord_).
39907+
1. Set _k_ to _k_ + 1.
39908+
1. Else,
39909+
1. NOTE: _asyncItems_ is neither an AsyncIterable nor an Iterable so assume it is an array-like object.
39910+
1. Let _arrayLike_ be ! ToObject(_asyncItems_).
39911+
1. Let _len_ be ? LengthOfArrayLike(_arrayLike_).
39912+
1. If IsConstructor(_C_) is *true*, then
39913+
1. Let _A_ be ? Construct(_C_, « 𝔽(_len_) »).
39914+
1. Else,
39915+
1. Let _A_ be ? ArrayCreate(_len_).
39916+
1. Let _k_ be 0.
39917+
1. Repeat, while _k_ &lt; _len_,
39918+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
39919+
1. Let _kValue_ be ? Get(_arrayLike_, _Pk_).
39920+
1. Set _kValue_ to ? Await(_kValue_).
39921+
1. If _mapping_ is *true*, then
39922+
1. Let _mappedValue_ be ? Call(_mapper_, _thisArg_, « _kValue_, 𝔽(_k_) »).
39923+
1. Set _mappedValue_ to ? Await(_mappedValue_).
39924+
1. Else,
39925+
1. Let _mappedValue_ be _kValue_.
39926+
1. Perform ? CreateDataPropertyOrThrow(_A_, _Pk_, _mappedValue_).
39927+
1. Set _k_ to _k_ + 1.
39928+
1. Perform ? Set(_A_, *"length"*, 𝔽(_len_), *true*).
39929+
1. Return _A_.
39930+
</emu-alg>
39931+
<emu-note>
39932+
<p>This method is an intentionally generic factory method; it does not require that its *this* value be the Array constructor. Therefore it can be transferred to or inherited by any other constructors that may be called with a single numeric argument.</p>
39933+
</emu-note>
39934+
</emu-clause>
39935+
3984639936
<emu-clause id="sec-array.isarray">
3984739937
<h1>Array.isArray ( _arg_ )</h1>
3984839938
<p>This function performs the following steps when called:</p>

0 commit comments

Comments
 (0)