企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
``` let num = 0 let list = [] function fetch (url) { return new Promise((resolve) => { const interval = Math.floor((Math.random() * 1000) + 1000) setTimeout(() => { console.log(url, interval) resolve(url) }, interval) }) } const limitFetch = async (url, pool = 4) => { num += 1 if(num > pool){ // 阻塞,放在一个数组list中,之后等4个执行完再执行 const awaitFetch = new Promise((resolve) => { list.push(resolve) }) await awaitFetch } await fetch(url) num -= 1 if(list.length){ const item = list.shift() item() } } limitFetch(1) limitFetch(2) limitFetch(3) limitFetch(4) limitFetch(5) limitFetch(6) limitFetch(7) limitFetch(8) ```