控制网络请求数
function limitNetworkRequest(arrRequest, limit, handler) {
const queueAll = Array.from(arrRequest);
const selectQueueOfIndex = queueAll.slice(0, limit).map((req, index) => {
return handler(req).then(() => index);
});
return queueAll
.reduce((preReq, currentReq) => {
return preReq
.then((promise) => {
return Promise.race(selectQueueOfIndex);
})
.then((index) => {
return (selectQueueOfIndex[index] = handler(currentReq).then(
() => index
));
})
.catch((err) => console.error(err));
}, Promise.resolve())
.then(Promise.all(queueAll));
}