Untitled
unknown
plain_text
a year ago
1.6 kB
3
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. * * 10 * -> 10 * * <-2 * * -> 8 */ function parallelHttpRequests(urls, limit, callback) { const results = Array(urls.length); let countResults = 0; let lastCalledUrlIdx = 0; limit = Math.min(urls.length, limit); const memForRepeated = {}; const uniqsUrls = new Set(); urls.forEach(url => { if (uniqsUrls.has(url)) { return memForRepeated[url] = null; } uniqsUrls.add(url); }) const fetchUrl = (urlIdx) => { const onSuccess = (res) => { results[urlIdx] = res; countResults++; if (countResults === urls.length) { return callback(results); } if (urls.length - 1 > lastCalledUrlIdx) { fetchUrl(++lastCalledUrlIdx) } } const url = urls[urlIdx]; if (typeof memForRepeated[url] === 'string') { return onSuccess(memForRepeated[url]); } fetch(url).then(onSuccess); } for (; lastCalledUrlIdx < limit; lastCalledUrlIdx++) { fetchUrl(lastCalledUrlIdx) } }
Editor is loading...
Leave a Comment