@mrli-utils/asyncpool
Version:
异步请求并发控制函数,限制同一时间发起的请求量
2 lines (1 loc) • 533 B
JavaScript
async function r(r,o,e,s=!1){const t=[],a=[];if("function"!=typeof e)throw Error("缺少真实请求函数");if(!Array.isArray(o))throw Error('asyncpool params "requestParams" should is array');for(const s of o){if(!Array.isArray(s))throw new TypeError("请求参数需要放到一个数组里面");const o=Promise.resolve().then((()=>e(...s)));if(t.push(o),t.length>=r){const e=o.then((()=>a.splice(a.indexOf(e),1)));a.push(e),r>a.length||await Promise.race(a)}}return s?Promise.allSettled(t):Promise.all(t)}export{r as asyncPool};