Skip to content

Commit

Permalink
Document EcmaScript-AsyncIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Feb 16, 2024
1 parent 7268cdc commit 9c606c1
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@ numbers.setProperty('later', 'value');
// 'value'
```

### Consuming an AsyncIterator as EcmaScript-AsyncIterator
Due to the syntactical sugar [EcmaScript's AsyncIterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator)
provides, our iterators can also be consumed as such.
If high performance over large iterators is required, this method of consumption not recommended.

```JavaScript
const numbers = new IntegerIterator({ start: 1, end: 100 });

for await (const number of numbers)
console.log('number', number);
console.log('all done!');
```

Error events emitted within the iterator can be caught by wrapping the for-await-block in a try-catch.

In cases where the returned EcmaScript AsyncIterator will not be fully consumed,
it is recommended to manually listen for error events on the main AsyncIterator
to avoid uncaught error messages.

## License
The asynciterator library is copyrighted by [Ruben Verborgh](http://ruben.verborgh.org/)
and released under the [MIT License](http://opensource.org/licenses/MIT).

0 comments on commit 9c606c1

Please sign in to comment.