@@ -234,15 +234,34 @@ const d = await Data.fromAsync(asyncGen(4));
234
234
### Optional parameters
235
235
Array.fromAsync has two optional parameters: ` mapfn` and ` thisArg` .
236
236
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.
241
240
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
243
247
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.
246
265
247
266
` thisArg` is a ` this ` -binding receiver value for the mapping callback. By
248
267
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.
452
471
## Other proposals
453
472
454
473
### 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
457
476
equivalent:
458
477
459
478
[iterator-helpers]: https://github.com/tc39/proposal-iterator-helpers
0 commit comments