0

I found that task.

Description

  1. It is necessary to execute all requests in the array in parallel.
  2. No more than 5 at a time.
  3. If the request returned an error, go ahead.
  4. If the 3rd returned the result, we make another request, and so on ...
  5. Finally, return an array of all successful queries.
const list = ["get0", "get1", "get2", "get3", "get4", "get5", "get6", "get7", "get8", "get9"];
const maxQuery = 5;

const customFetch = (url) =>
    new Promise((resolve, reject) => {
        if (Math.random() < 0.2) {
            setTimeout(() => reject("Error: " + url), (Math.random() * 1500) | 0);
        } else {
            setTimeout(() => resolve("Some data: " + url), (Math.random() * 500) | 0);
        }
    });

async function getData(list) {
    // ...
    // some code
    // ...
}

getData(list).then((result) => console.log(result));

But I haven't the solution. I guess I could be use Promise and cycle "for of" for work with async operations. But I can't combinate it. Please any solutions for this

pilchard
  • 9,782
  • 5
  • 9
  • 21
pridan
  • 161
  • 6

0 Answers0