@hm-soft/queue
v0.1.0
Published
동시 실행 제한(concurrency limiter) — createLimiter / mapLimit 으로 대량 비동기 작업을 N개씩 처리 (의존성 0)
Downloads
83
Maintainers
Readme
@hm-soft/queue
동시 실행 제한(concurrency limiter). 한 번에 최대 N개의 비동기 작업만 실행해 과부하·외부 API rate limit을 막습니다. 의존성 0.
설치
npm install @hm-soft/queue사용
import { createLimiter, mapLimit } from "@hm-soft/queue";
// 동시 5개씩만 — 대량 API 호출
const limit = createLimiter(5);
const results = await Promise.all(urls.map((u) => limit(() => fetchJson(u))));
// 배열 매핑 (순서 보존)
const thumbs = await mapLimit(images, 3, (img) => resize(img));
// 진행 상황
limit.activeCount; // 실행 중
limit.pendingCount; // 대기 중API
| 함수 | 설명 |
|------|------|
| createLimiter(concurrency) | limiter 함수 반환. limit(fn)로 작업 등록 |
| limit(fn) | 동시 실행 한도 안에서 fn 실행, 결과 Promise |
| limit.activeCount / limit.pendingCount | 실행/대기 수 |
| mapLimit(items, concurrency, mapper) | 배열을 N개씩 매핑(순서·인덱스 보존, 첫 에러로 reject) |
- 작업이 throw하면 해당 Promise가 reject되고, limiter는 계속 다음 작업을 진행합니다.
mapLimit은 결과를 입력 순서대로 반환하며, 하나라도 실패하면 전체가 reject됩니다.
License
Apache-2.0
