Skip to content

fx-flow v0.7.0 / pipe/lazy / concurrent

Function: concurrent()

concurrent(length, iterable)

concurrent<A>(length, iterable): AsyncIterableIterator<A>

Concurrent is used to balance the load of multiple asynchronous requests. It controls the number of concurrency for iterable.

Type parameters

A

Parameters

length: number

iterable: AsyncIterable<A>

Returns

AsyncIterableIterator<A>

Example

typescript
await pipe(
  [1, 2, 3, 4, 5, 6],
  toAsync,
  map((a) => delay(1000, a)),
  concurrent(3),
  each(console.log), // log 1, 2, 3, 4, 5, 6
); // takes 2 seconds
// Task 1, 2, 3 start together, and task 4, 5, 6 wait for all the three task finished, then start together.

await pipe(
  [1, 2, 3, 4, 5, 6],
  toAsync,
  map((a) => delay(1000, a)),
  each(console.log), // log 1, 2, 3, 4, 5, 6
); // takes 6 seconds
// Tasks start sequentially, each task starts after the previous one finished.

More examples

Source

packages/core/src/pipe/lazy/concurrent.ts:46

concurrent(length)

concurrent<A>(length): (iterable) => AsyncIterableIterator<A>

Type parameters

A

Parameters

length: number

Returns

Function

Parameters

iterable: AsyncIterable<A>

Returns

AsyncIterableIterator<A>

Source

packages/core/src/pipe/lazy/concurrent.ts:48

Released under the MIT License.