https://github.com/hkk12369/async-iter-utils
Useful utilities for js async iterators
https://github.com/hkk12369/async-iter-utils
Last synced: 2 months ago
JSON representation
Useful utilities for js async iterators
- Host: GitHub
- URL: https://github.com/hkk12369/async-iter-utils
- Owner: hkk12369
- License: mit
- Created: 2021-05-13T13:11:28.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-05-13T13:38:11.000Z (about 4 years ago)
- Last Synced: 2025-03-05T15:18:26.088Z (3 months ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## async-iter-utils
Useful utilities for js async iterators.## Install
```sh
npm install async-iter-utils
# OR
yarn add async-iter-utils
```## Docs
#### `onItem(iterable, fn)`
convert an async iterator to callback style
```js
const {onItem} = require('async-iter-utils');onItem(iterable, (item) => {
console.log(item);
})
```#### `forEach(iterable, fn, {concurrency = 1, stopOnError = true} = {})`
Run a function for each item of async iterable with given `concurrency`. If `stopOnError` is false, all errors will be collected and returned as an `AggregateError`, otherwise it'll stop on any error.
```js
const {forEach} = require('async-iter-utils');await forEach(iterable, async (item) => {
console.log(await process(item));
}, {concurrency: 10});
```#### `map(iterable, fn, {concurrency = 1, stopOnError = true} = {})`
Same as `forEach` but will collect and return all results as an array.
```js
const {forEach} = require('async-iter-utils');const results = await map(iterable, async (item) => {
return process(item);
}, {concurrency: 10});
```#### `toArray(iterable, {concurrency = 1} = {})`
Convert an async iterable to an array.
```js
const {toArray} = require('async-iter-utils');const arr = await toArray(iterable);
```#### `chunk(iterable, {chunkSize = 1} = {})`
Convert an async iterable to another async iterable of chunkSize.
```js
const {chunk} = require('async-iter-utils');const chunkedIterator = chunk(iterable, {chunkSize: 10});
for await (const chunks of chunkedIterator) {
await Promise.all(chunks);
}
```