JS| How to use Promise.all to handle thousands of request

import PromisePool from 'es6-promise-pool';


// macs - Array of mac addresses
useEffect(() => {
const fetchData = () => {
const results = [];
const generatePromises = function*() {
for (let count = 0; count < macs.length; count++) {
yield axios.get(`/ipdn/${macs[count]}`, ...);
}
}
const promiseIterator = generatePromises();

// Create a pool with 10 concurrent requests max
const pool = new PromisePool(
promiseIterator,
10 // Configurable
);

// To listen to result
pool.addEventListener('fulfilled', function (event) {
console.log('Fulfilled: ' + event.data.result);
results.push(event.data.result);
});
// Start the pool
pool.start().then(function () {
setData(results);
console.log('Complete');
});
};
fetchData();
}, []);
function App() {
const [data, setData] = useState([]);

useEffect(() => {
// helper function, split array to chunked array
const splitToChunks = (items, chunkSize = 50) => {
const result = [];
for (let i = 0; i < items.length; i += chunkSize) {
result.push(items.slice(i, i + chunkSize));
}
return result;
}

const fetchData = async () => {
const result = []; // init value
const macAddresses = []; // array of mac addresses - your mac addresses
// return array of array [[...50 mac adds], [], []]
const chunkedArray = splitToChunks(macAddresses);


for (const macs of chunkedArray) {
// now macs is [...50 mac adds]
const promises = macs.map((mac) => {
return axios.get(`/ipdn/${mac}`, { timeout: 10000 });
});
// now promises is array contains 50 Promises
// wait until finish 50 requests
const response = await Promise.all(promises);

// copy response to result, and continue for next block
result.push(...response);
}

setData(result);
};
fetchData();
}, []);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store