Skip to content

Commit 304bbe9

Browse files
committed
docs: Elaborate on parallels with Array.from
1 parent 541f23d commit 304bbe9

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,37 @@ Since its standardization in JavaScript, **[Array.from][]** has become one of
1717
`Array`’s most frequently used built-in methods. However, no similar
1818
functionality exists for async iterators.
1919

20+
```js
21+
const arr = [];
22+
for (const v of iterable) {
23+
arr.push(v);
24+
}
25+
26+
// This does the same thing.
27+
const arr = Array.from(iterable);
28+
```
29+
2030
[Array.from]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
2131

22-
Such functionality would be useful for **dumping** the entirety of an **async
23-
iterator** into a **single data structure**, especially in **unit tests** or in
24-
**command-line** interfaces. (Several real-world examples are included in a
25-
following section.)
32+
Such functionality would also be useful for **dumping** the entirety of an
33+
**async iterator** into a **single data structure**, especially in **unit
34+
tests** or in **command-line** interfaces. (Several real-world examples are
35+
included in a following section.)
2636

27-
There is an [it-all][] NPM library that performs only this task
28-
and which gets about 50,000 weekly downloads daily.
29-
This of course does **not** include any code
30-
that uses ad-hoc `for await``of` loops with empty arrays:
3137
```js
3238
const arr = [];
3339
for await (const v of asyncIterable) {
3440
arr.push(v);
3541
}
42+
43+
// We should add something that does the same thing.
44+
const arr = await ??????????(asyncIterable);
3645
```
46+
47+
There is an [it-all][] NPM library that performs only this task
48+
and which gets about 50,000 weekly downloads daily.
49+
This of course does **not** include any code
50+
that uses ad-hoc `for await``of` loops with empty arrays.
3751
Further demonstrating the demand for such functionality,
3852
several [Stack Overflow questions][Stack Overflow] have been asked
3953
by various developers, asking how to convert async iterators to arrays.
@@ -47,8 +61,8 @@ later in this explainer.
4761
## Description
4862
(A [formal draft specification][specification] is available.)
4963
50-
Array.fromAsync is to `for await`\
51-
as Array.from is to `for`.
64+
**Array.fromAsync is to `for await`**\
65+
as **Array.from is to `for`.**
5266
5367
Similarly to [Array.from][], Array.fromAsync would be a static method of the
5468
`Array` built-in class, with one required argument and two optional arguments:

0 commit comments

Comments
 (0)