← 中文资源

简体中文版本

固定并发 Promise 池

pLimit 风格的并发限制器:结果保持输入顺序、拒绝向上传播、并发为一时严格串行。

阅读英文原文

目标

实现固定并发池:活跃数小于限额即启动任务,超限则排队;结果按输入顺序返回,任一拒绝传播给调用方。

要点

并发峰值测试证明 20 个任务限 4 时峰值恰为 4;串行测试断言 start/end 交替不重合;demo 十个任务过三并发池,输出 peak=3 与平方结果序列。

提示

没有取消也没有运行中动态提交——提交固定数组并等待结果。

验证

node –test 四个用例;demo 输出 peak=3 results=1,4,9,…,100。 复现命令:

node --test pool.test.mjs
node pool.mjs

预期输出要点:The demo runs ten tasks through a pool of three, printing the observed peak concurrency as exactly 3 and all ten results in order.

实测证据:Node 22.23.2: node –test -> 4 passed; demo reported peak=3 across ten squared results returned in input order.