Skip to content

Commit 2bf3c7c

Browse files
authored
docs: Add example and link for mapping-function behavior
1 parent f6a94d2 commit 2bf3c7c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ const d = await Data.fromAsync(asyncGen(4));
234234
### Optional parameters
235235
Array.fromAsync has two optional parameters: `mapfn` and `thisArg`.
236236
237+
#### Mapping function
237238
`mapfn` is an optional mapping callback, which is called on each value yielded from the input,
238239
along with its index integer (starting from 0).
239240
Each result of the mapping callback is, in turn, awaited then added to the array.
@@ -263,6 +264,33 @@ This is because, whenever input is an async iterable that yields promise items,
263264
but `Array.fromAsync(input, x => x)` will resolve them
264265
because the result of the `x => x` mapping function is awaited.
265266
267+
For example:
268+
269+
```js
270+
function as () {
271+
return {
272+
[Symbol.asyncIterator]() {
273+
return {
274+
async next() {
275+
if (i > 2) return { done: true };
276+
i++;
277+
return { value: Promise.resolve(i), done: false }
278+
}
279+
}
280+
}
281+
};
282+
}
283+
284+
// This prints `[Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]`:
285+
console.log(await Array.fromAsync(it));
286+
287+
// This prints `[1, 2, 3]`:
288+
console.log(await Array.fromAsync(it, x => x));
289+
```
290+
291+
See also [issue #19](https://github.com/tc39/proposal-array-from-async/issues/19).
292+
293+
#### `this` argument
266294
`thisArg` is a `this`-binding receiver value for the mapping callback. By
267295
default, this is undefined. These optional parameters match the behavior of
268296
Array.from. Their exclusion would be surprising to developers who are already

0 commit comments

Comments
 (0)