I found that task.
Description
- It is necessary to execute all requests in the array in parallel.
- No more than 5 at a time.
- If the request returned an error, go ahead.
- If the 3rd returned the result, we make another request, and so on ...
- 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