Function multiRequestWithLimit(payloadList, handler, limit = 3) {const len = payloadList.length; // Create an Array based on the number of requests const result = new Array(len).fill(false); // let index = 0; return new Promise((resolve, reject) => { for (let i = 0; i < limit; i++) { next(); } function next() { if (! result.includes(false)) { return resolve(result); } if (index > len - 1) { return; } handler(payloadList[index]) .then((res) => (result[index] = res)) .catch((err) => (result[index] = err)) .finally((final) => { console.log("final", final); index++; }) .then(next); }}); } const payloadList = [1, 2, 3, 4]; const handle = (a) => { setTimeout(() => { console.log(a); }, 200); }; multiRequestWithLimit(payloadList, handle).then(res => console.log(res))Copy the code