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