一、实现思路
主要是对请求进行分组,等分组请求有返回结果了,然后在进行下次分组请求,主要采用递归的形式
二、 案例代码
packagFunc.js
import { BATCH_SIZE } from './constants';
/**
* Batch the request and delay 3 seconds every 1000 requests
* @param {*} records
* @param {*} promiseEach
* @returns
*/
export function batchRequest(records, promiseEach) {
// Divide the total requests into sub request arrays by the batch size
const batchRecords = [];
for (let i = 0; i < records.length; i += BATCH_SIZE) {
batchRecords.push(records.slice(i, i + BATCH_SIZE));
}
const result = [];
let index = 0;
return new Promise((resolve, reject) => {
const request = (resolveObj, rejectObj) => {
if (index < batchRecords.length) {
const requestList = [];
batchRecords[index].forEach((item) => {
requestList.push(promiseEach(item));