function multiRequest(urls, max) { return new Promise((resolve, reject) => { const requestRes = {} const len = urls.length //总大小 let finshedCount = 0 //已完成 let running = 0 // 正在请求中 let index = 0 function addQueue() { index++ running += 1 fetch(urls[index]).then(res => { requestRes[index - max] = res }).catch(err => { requestRes[index - max] = err }).finally(() => { running -= 1 finshedCount += 1 if (running < max && finshedCount < len) { addQueue() } if (finshedCount == len) { resolve(Array.from({ ...requestRes, length: len })) } }) if (running < max && finshedCount < len) { addQueue() } } addQueue() }) }
点赞 评论

相关推荐

牛客网
牛客企业服务