https://github.com/terkelg/simultan
Simultaneously run an async function on any iterable with limited concurrency
https://github.com/terkelg/simultan
async concurrency concurrent iterator promises
Last synced: 9 months ago
JSON representation
Simultaneously run an async function on any iterable with limited concurrency
- Host: GitHub
- URL: https://github.com/terkelg/simultan
- Owner: terkelg
- License: mit
- Created: 2021-01-25T20:22:58.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-27T12:26:35.000Z (about 5 years ago)
- Last Synced: 2024-11-08T16:13:08.387Z (about 1 year ago)
- Topics: async, concurrency, concurrent, iterator, promises
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license
Awesome Lists containing this project
README
# Simultan [](https://github.com/terkelg/simultan/actions) [](https://npmjs.org/package/simultan)
> Simultaneously run an async function on any iterable with limited concurrency.
Simultan takes an iterable, executes an async method on each value, and returns a single `Promise`. The promise contains an array of all the resolved return values. The number of concurrent invocations can be limited.
## Install
```
$ npm install --save simultan
```
## Usage
```js
import { simultan } from 'simultan';
const urls = [
// ... array of urls to fetch
]
await simultan(urls, async url => {
const response = fetch(url);
return response.json();
});
//=> [{...}, {...}, {...}, ]
```
## API
### simultan(iterable, fn, limit = 200)
Returns: `Promise`
Simultan executes an async callback on each value of any [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols). A single `Promise` is returned with an array of the resolved return values.
This returned promise will resolve when all invocations of the callback method have been resolved.
#### iterable
Type: `Iterable | AsyncIterable`
Required: `true`
Iterable to iterate and execute async callback function on.
#### fn
Type: `IteratorFn = (item: T) => Promise`
Required: `true`
#### limit
Type: `number`
Default: `200`
Required: `false`
Maximum number of concurrent invocations.
## License
MIT © [Terkel Gjervig](https://terkel.com)