chunk(array, size) { const chunks = [] let index = 0 while (index < array.length) { chunks.push(array.slice(index, size + index)) index += size } return chunks },
promiseAllSettled(promises) { return Promise.all( promises.map(promise => Promise.resolve(promise).then( value => ({ status: 'fulfilled', value }), reason => ({ status: 'rejected', reason }) ) ) ); },
async batchApprove() {
let promiseArray = [] promiseArray = idList.map(item =>{ return { id: item, // 序号id } }) let results = [] const promiseChunks = this.chunk(promiseArray, 5) // 切分成 n 个请求为一组 let groupIndex = 1 try { // 调取接口 for (let ckg of promiseChunks) { // -- 修改后新增逻辑(在发起一组请求时,收集该组对应的 Promiise 成员) const ck = ckg.map(idx => batchApprove(idx).then((res)=>{ Progress++;console.log(`Progress: ${Progress / idList.length * 100}%`);this.Progress = Math.round(`${Progress / idList.length * 100}` * 10) / 10; return Promise.resolve(res) }).catch((err) => { console.log(err); return Promise.reject(err); }) ) // 发起一组请求 const groupResult = await this.promiseAllSettled(ck) // 控制并发数 console.log(groupResult,'groupResult') results.push(...groupResult) console.log(`------ 第${groupIndex}组分批发起完毕 --------`) groupIndex += 1 } // console.log('所有请求的结果:', results); }catch(err){
} }