来源:
import axios from 'axios'
export const handQueue = (
list // 请求总数
) => {
list = list || 0
const requestQueue = (maxNum = 6) => {
const queue = [];
let current = 0 // 当前请求了多少条
const dequeue = () => {
while (current < maxNum && queue.length) {
current++
const currentPromise = queue.shift() // 出列
currentPromise()
.then(() => {
// 成功的请求逻辑
console.log('执行成功')
})
.catch((error) => {
// 失败
console.log(error)
})
.finally(() => {
current--
dequeue()
})
}
}
return (currentPromise) => {
queue.push(currentPromise) // 入队
dequeue()
}
}
const enqueue = requestQueue(6)
for (let i = 0; i < list; i++) {
enqueue(() => axios.get('/api/test' + i))
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容