Skip to content

Simultaneously run an async function on any iterable with limited concurrency

License

Notifications You must be signed in to change notification settings

terkelg/simultan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simultan CI npm

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

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<T, R = unknown>(iterable, fn, limit = 200)

Returns: Promise<unknown[]>

Simultan executes an async callback on each value of any Iterable. 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<T> | AsyncIterable<T>
Required: true

Iterable to iterate and execute async callback function on.

fn

Type: IteratorFn<T, R> = (item: T) => Promise<R>
Required: true

limit

Type: number
Default: 200
Required: false

Maximum number of concurrent invocations.

License

MIT © Terkel Gjervig

About

Simultaneously run an async function on any iterable with limited concurrency

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published