Untitled

mail@pastecode.io avatar
unknown
javascript
2 months ago
985 B
4
Indexable
Never
/**
 * 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) {
  // TODO: Implement the function

  const result = [];
  for (let i = 0; i < urls.length; i += limit) {
    for (let index = i; index < i + limit; index++) {
      if (urls[index]) {
        // fetch(urls[index]).then((data) => (result[index] = data))
        Promise.all( [...fetch(urls[index])]);
      }
    }
  }
  callback(result);
}
Leave a Comment