From f7dd1e5d6e6bf5f329d972e8e561bcf71979b9f3 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Fri, 10 Nov 2023 11:18:38 +0100 Subject: [PATCH] Add back deleted examples Removed as part of https://github.com/tc39/proposal-iterator-helpers/pull/263 --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 3db1922..a9d0988 100644 --- a/README.md +++ b/README.md @@ -60,3 +60,38 @@ This proposal introduces two new intrisic objects, in addition to the two added const AsyncIteratorHelperPrototype = Object.getPrototypeOf(AsyncIterator.from([]).take(0)); const WrapForValidAsyncIteratorPrototype = Object.getPrototypeOf(AsyncIterator.from({ async next(){} })); ``` + +## More Example Usage + +### Lazy Iteration over sets + +Iterating over a set of URLs, asynchronously fetching each, and returning an array of their +JSON Output. + +```js +const responses = await AsyncIterator.from(urls) + .map(async (url) => { + const response = await fetch(url); + return response.json(); + }) + .toArray(); +``` + +Example of iterating over a potentially infinite iterator and transforming it to an array in groups +of 5. + +```js +class ObligatoryCryptocurrencyReference extends Component { + componentWillMount() { + const items = ticker() // returns async iterator + .map((c) => createElement('h2', null, `${c.name}: ${c.price}`)) + .take(5) // only consume 5 items of a potentially infinite iterator + .toArray() // greedily transform async iterator into array + .then((data) => this.setState({ data })); + } + + render() { + return createElement('div', null, this.state.data); + } +} +```