|
| 1 | +# Async Iterator Helpers |
| 2 | + |
| 3 | +A proposal for several interfaces that will help with general usage and |
| 4 | +consumption of async iterators in ECMAScript. |
| 5 | + |
| 6 | +This proposal was split out from [proposal-iterator-helpers](https://github.com/tc39/proposal-iterator-helpers) to resolve design questions related to parallelism, which are not relevant to sync helpers. Many design questions (including choice of methods) were discussed and decided in that respository, and its readme should be read first. |
| 7 | + |
| 8 | +## Status |
| 9 | + |
| 10 | +Authors: Gus Caplan, Michael Ficarra, Adam Vandolder, Jason Orendorff, Kevin Gibbons |
| 11 | + |
| 12 | +Champions: Michael Ficarra, Kevin Gibbons |
| 13 | + |
| 14 | +This proposal is at Stage 2 of [The TC39 Process](https://tc39.es/process-document/). |
| 15 | + |
| 16 | +**This proposal is in the process of being revised.** The core set of helpers and their high-level API is unlikely to change, but the underlying specification mechanism will likely be radically revised. |
| 17 | + |
| 18 | +This proposal contains the following methods: |
| 19 | + |
| 20 | +- `Iterator.prototype.toAsync` |
| 21 | +- `AsyncIterator.from` |
| 22 | +- `AsyncIterator.prototype` |
| 23 | + - `.map` |
| 24 | + - `.filter` |
| 25 | + - `.take` |
| 26 | + - `.drop` |
| 27 | + - `.flatMap` |
| 28 | + - `.reduce` |
| 29 | + - `.toArray` |
| 30 | + - `.forEach` |
| 31 | + - `.some` |
| 32 | + - `.every` |
| 33 | + - `.find` |
| 34 | + |
| 35 | +See [proposal-iterator-helpers](https://github.com/tc39/proposal-iterator-helpers) for motivation and additional high-level descriptions for these methods. |
| 36 | + |
| 37 | +## Parallelism |
| 38 | + |
| 39 | +In the iterator-producing methods (`.map`, `.filter`, `.take`, `.drop`, and `.flatMap`), async helpers have the opportunity to support _parallelism_. For example, in the following code: |
| 40 | + |
| 41 | +```js |
| 42 | +asyncIteratorOfUrls |
| 43 | + .map(u => fetch(u)) |
| 44 | + |
| 45 | +await Promise.all([ |
| 46 | + x.next(), |
| 47 | + x.next(), |
| 48 | +]) |
| 49 | +``` |
| 50 | + |
| 51 | +there could be two calls to `fetch` running at once. For this to work, the helpers have to be explicitly designed to support this. The original design of this proposal instead implemented the helpers as essentially async generators, which buffer calls to `.next` rather than allowing multiple calls to have simulaneous effects. |
| 52 | + |
| 53 | +This proposal is being revised to support at least the above use case. The exact details of what that looks like for each helper are not yet decided. |
| 54 | + |
| 55 | +### How can I access the new intrinsics? |
| 56 | + |
| 57 | +This proposal introduces two new intrisic objects, in addition to the two added in the sync proposal. They can be accessed as follows: |
| 58 | + |
| 59 | +```js |
| 60 | +const AsyncIteratorHelperPrototype = Object.getPrototypeOf(AsyncIterator.from([]).take(0)); |
| 61 | +const WrapForValidAsyncIteratorPrototype = Object.getPrototypeOf(AsyncIterator.from({ async next(){} })); |
| 62 | +``` |
0 commit comments