concurrent() function
Concurrent는 여러 비동기 요청의 부하를 분산하는 데 사용됩니다. 첫 번째 인자는 부하 수를 제어하는 숫자를 받고, 두 번째 인자는 AsyncIterable입니다. AsyncIterable을 만들려면 toAsync를 참조하세요.
Signature:
typescript
declare function concurrent<A>(
length: number,
iterable: AsyncIterable<A>,
): AsyncIterableIterator<A>;Example
ts
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
); // 2 seconds
// evaluation
// ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
// │ 1 │──│ 2 │──│ 3 │──│ 4 │──│ 5 │──│ 6 │
// └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘
// map │ │ │ │ │ │
// concurrent(3) (1) (1) (1) (2) (2) (2)
// │ │ │ │ │ │
// ▼ ▼ ▼ ▼ ▼ ▼
await pipe(
[1, 2, 3, 4, 5, 6],
toAsync,
map((a) => delay(1000, a)),
each(console.log), // log 1, 2, 3, 4, 5, 6
); // 6 seconds
// evaluation
// ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
// │ 1 │──│ 2 │──│ 3 │──│ 4 │──│ 5 │──│ 6 │
// └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘
// map │ │ │ │ │ │
// (1) (2) (3) (4) (5) (6)
// │ │ │ │ │ │
// ▼ ▼ ▼ ▼ ▼ ▼