Skip to content

Commit f6a94d2

Browse files
authored
docs: Clarify mapping function behavior
See #19.
1 parent 7f0d666 commit f6a94d2

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,34 @@ const d = await Data.fromAsync(asyncGen(4));
234234
### Optional parameters
235235
Array.fromAsync has two optional parameters: `mapfn` and `thisArg`.
236236
237-
`mapfn` is an optional mapping callback, which is called on each value yielded
238-
from the input (and awaited if it came from a synchronous input), along with
239-
its index integer (starting from 0). Each result of the mapping callback is, in
240-
turn, awaited then added to the array.
237+
`mapfn` is an optional mapping callback, which is called on each value yielded from the input,
238+
along with its index integer (starting from 0).
239+
Each result of the mapping callback is, in turn, awaited then added to the array.
241240
242-
(Without the optional mapping callback, each value yielded from asynchronous
241+
However, when `mapfn` is given and the input is a sync iterable (or non-iterable array-like),
242+
then each value from the input is awaited before being given to `mapfn`.
243+
(The values from the input are *not* awaited if the input is an async iterable.)
244+
This matches the behavior of `for await`.
245+
246+
When `mapfn` is not given, each value yielded from asynchronous
243247
inputs is not awaited, and each value yielded from synchronous inputs is
244-
awaited only once, before the value is added to the result array. This matches
245-
the behavior of `for await`.)
248+
awaited only once, before the value is added to the result array.
249+
This also matches the behavior of `for await`.
250+
251+
This means that:
252+
```js
253+
Array.fromAsync(input)
254+
```
255+
…is not equivalent to:
256+
```js
257+
Array.fromAsync(input, x => x)
258+
```
259+
…at least when `input` is an async iterable.
260+
261+
This is because, whenever input is an async iterable that yields promise items,
262+
`Array.fromAsync(input)` will not resolve those promise items,
263+
but `Array.fromAsync(input, x => x)` will resolve them
264+
because the result of the `x => x` mapping function is awaited.
246265
247266
`thisArg` is a `this`-binding receiver value for the mapping callback. By
248267
default, this is undefined. These optional parameters match the behavior of
@@ -452,8 +471,8 @@ when the sync iterator yields a rejected promise as its next value.
452471
## Other proposals
453472
454473
### Relationship with iterator-helpers
455-
The [iterator-helpers][] and [async-iterator-helpers][] proposal define
456-
Iterator.toArray and AsyncIterator.toArray. The following pairs of lines are
474+
The [iterator-helpers][] and [async-iterator-helpers][] proposals define
475+
Iterator.toArray and AsyncIterator.toArray. The following pairs of lines are
457476
equivalent:
458477
459478
[iterator-helpers]: https://github.com/tc39/proposal-iterator-helpers

0 commit comments

Comments
 (0)