Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
0
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 data = await fetch()
  let counter = 0;
  const result = [];

  if (!urls.length) {
    return;
  }

  function getUrls() {
    let counterLimit = 0

    fetch(urls[counter]).then((response) => {
      result[counter] = response
      getUrls()
    });
    counter++;

    if (counter !== urls.length - 1) {
      getUrls()
    }
  }


  parallelHttpRequests(urls, limit, callback);

  for (let i = 0; i < urls.length; i++) {
    const url = urls[i];

    const response = fetch(url).then(() => {
      counter--;
    });
  }
}

// counter += 1
// counter === limit

const urls = ["google.com", "yandex.ru", "amz.com"];
const limit = 2;
// google.com + google.com = 1 rq 
// 
function callback() {}
Leave a Comment