function conurRequest(urls = [],max = 0){
return new Promise(res=>{
let index = 0
const reqs = []
if (!urls.length){
res([])
}
async function request(){
const i = index
index ++
try {
reqs[i] = await (await fetch(urls[i])).json()
} catch (error) {
reqs[i] = error
}finally{
if(index < urls.length){
request()
}
if(reqs.length==urls.length){
res(reqs)
}
}
}
for(let i = 0 ; i < Math.min(urls.length , max) ; i++){
request()
}
})
}
使用
const urls = []
for(i=0 ; i<100 ; i++){
urls[i] = 'http://localhost:4000/testrequest'//调用的接口集合
}
// console.log(urls.length)
conurRequest(urls,10).then(reqs =>{
console.log(reqs)
})