Skip to content

Commit 64a4921

Browse files
committed
explainer/spec: Sync iterators and array-like objects
Fixes #6. Also addresses #7.
1 parent a76f4ea commit 64a4921

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ Similarly to **[`Array.from`][]**,
4646
of the `Array` built-in class, with **one required argument**
4747
and **two optional arguments**: `(items, mapfn, thisArg)`.
4848

49-
But instead of converting an **iterable** to an array,
50-
it converts an **async iterable** to a **promise**
51-
that will resolve to an array.
49+
But instead of converting an **array-like object** or **iterable** to an array,
50+
it converts an **async iterable** (or array-like object or iterable)
51+
to a **promise** that will resolve to an array.
5252

5353
```js
5454
async function * f () {

spec.html

+33-14
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ <h1><ins>Array.fromAsync ( _asyncItems_ [ , _mapfn_ [ , _thisArg_ ] ] )</ins></h
4343
<emu-alg>
4444
1. Let _C_ be the *this* value.
4545
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
46-
1. If _mapfn_ is *undefined*, let _mapping_ be *false*.
47-
1. Else,
48-
1. If IsCallable(_mapfn_) is *false*, throw a *TypeError* exception.
49-
1. Let _mapping_ be *true*.
50-
1. Let _usingAsyncIterator_ be ? GetMethod(_asyncItems_, @@asyncIterator).
51-
1. If _usingAsyncIterator_ is *undefined*, throw a *TypeError* exception.
52-
1. Else,
53-
1. If IsConstructor(_C_) is *true*, then
54-
1. Let _A_ be ? Construct(_C_).
46+
1. Let _fromAsyncClosure_ be a new Abstract Closure with no parameters that captures _C_ and _mapfn_ and performs the following steps when called:
47+
1. If _mapfn_ is *undefined*, let _mapping_ be *false*.
48+
1. Else,
49+
1. If IsCallable(_mapfn_) is *false*, throw a *TypeError* exception.
50+
1. Let _mapping_ be *true*.
51+
1. Let _usingAsyncOrSyncIterator_ be ? GetMethod(_asyncItems_, @@asyncIterator).
52+
1. If _usingAsyncOrSyncIterator_ is *undefined*, let _usingAsyncOrSyncIterator_ be ? GetMethod(_asyncItems_, @@iterator).
53+
1. If _usingAsyncOrSyncIterator_ is *undefined*, throw a *TypeError* exception.
5554
1. Else,
56-
1. Let _A_ be ! ArrayCreate(0).
57-
1. Let _iteratorRecord_ be ? GetIterator(_asyncItems_, ~async~, _usingAsyncIterator_).
58-
1. Let _asyncSpreadClosure_ be a new Abstract Closure with no parameters that captures _A_ and _mapfn_ and performs the following steps when called:
55+
1. If IsConstructor(_C_) is *true*, then
56+
1. Let _A_ be ? Construct(_C_).
57+
1. Else,
58+
1. Let _A_ be ! ArrayCreate(0).
59+
1. Let _iteratorRecord_ be ? GetIterator(_asyncItems_, ~async~, _usingAsyncOrSyncIterator_).
5960
1. Let _k_ be 0.
6061
1. Repeat,
6162
1. If _k_ &ge; 2<sup>53</sup> - 1, then
@@ -77,8 +78,26 @@ <h1><ins>Array.fromAsync ( _asyncItems_ [ , _mapfn_ [ , _thisArg_ ] ] )</ins></h
7778
1. Let _defineStatus_ be CreateDataPropertyOrThrow(_A_, _Pk_, _mappedValue_).
7879
1. If _defineStatus_ is an abrupt completion, return ? AsyncIteratorClose(_iteratorRecord_, _defineStatus_).
7980
1. Set _k_ to _k_ + 1.
80-
1. Perform AsyncFunctionStart(promiseCapability, _asyncSpreadClosure_).
81-
1. Return Completion { [[Type]]: ~return~, [[Value]]: _promiseCapability_.[[Promise]], [[Target]]: ~empty~ }.
81+
1. NOTE: _items_ is not an AsyncIterable or Iterable so assume it is an array-like object.
82+
1. Let _arrayLike_ be ! ToObject(_items_).
83+
1. Let _len_ be ? LengthOfArrayLike(_arrayLike_).
84+
1. If IsConstructor(_C_) is *true*, then
85+
1. Let _A_ be ? Construct(_C_, &laquo; 𝔽(_len_) &raquo;).
86+
1. Else,
87+
1. Let _A_ be ? ArrayCreate(_len_).
88+
1. Let _k_ be 0.
89+
1. Repeat, while _k_ &lt; _len_,
90+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
91+
1. Let _kValue_ be ? Get(_arrayLike_, _Pk_).
92+
1. If _mapping_ is *true*, then
93+
1. Let _mappedValue_ be ? Call(_mapfn_, _thisArg_, &laquo; _kValue_, 𝔽(_k_) &raquo;).
94+
1. Else, let _mappedValue_ be _kValue_.
95+
1. Perform ? CreateDataPropertyOrThrow(_A_, _Pk_, _mappedValue_).
96+
1. Set _k_ to _k_ + 1.
97+
1. Perform ? Set(_A_, *"length"*, 𝔽(_len_), *true*).
98+
1. Return _A_.
99+
1. Perform AsyncFunctionStart(promiseCapability, _fromAsyncClosure_).
100+
1. Return Completion { [[Type]]: ~return~, [[Value]]: _promiseCapability_.[[Promise]], [[Target]]: ~empty~ }.
82101
</emu-alg>
83102
<emu-note>
84103
<p>The `fromAsync` function 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>

0 commit comments

Comments
 (0)