Untitled
unknown
plain_text
a year ago
1.0 kB
4
Indexable
/** * Makes parallel HTTP requests to the given array of URLs, with a constraint on the number * of simultaneous requests allowed. Once all requests are completed, calls the provided * callback function with an array of text responses in the same order as the input URLs. * * @param {string[]} urls - An array of URLs to make HTTP requests to. * @param {number} limit - The maximum number of simultaneous HTTP requests allowed. * @param {function(string[]): void} callback - The callback function to be called with an * array of text responses once all requests are completed. */ function parallelHttpRequests(urls, limit, callback) { // const numberOfIterations = Math.ceil(urls / limit); const urlsCopy = [...urls]; const finalResult = [] const sendRequests = () => Promise.all(urlsCopy.slice(0, limit).map(url => fetch(url))).then((resul) => { finalResult.push(resul); urlsCopy.splice(0, limit); if (urlsCopy.length) { sendRequests(); } }); sendRequests().finaly(result => callback(result)) }
Editor is loading...
Leave a Comment