I have an array of Promises that should be run in parallel and be finished as soon as possible. We have a Promise.all(arrayOfPromises)
for that. But I'd like to have a kind of a modified Promise.all that can work with a dynamic array. Let me expalain what I mean.
I start with an array of 215 URLs (urlsArray
) that I should send a request to. But I can process only 100 requests at a time. So I take the first 100 URLs and create a fetchArray
array of those 100 fetch'es and then pass it to Promise.all(fetchArray)
.
Now let's say one of the Promises resolved. I'd like to remove it from the fetchArray
and then put the 101st fetch from urlsArray
into fetchArray
.
I could use Promise.race() for this. But it would interrupt all other requests so I would lose all the progess. And it's not what I want since I have to get responses form all 215 URLs as soon as possible.
Also while processing the current requests, new URLs could be added to the urlsArray
array. Potentially the urlsArray
could be endless and new URLs could be added to the array again and again. So fetchArray
should replace the just resolved Promise with the next one from urlsArray
(if any) over and over again without losing the progress of other requests.
Is it even possible to implement this? Could you please give me any ideas, hints or code? Maybe some articles or tutorials about how to implement this?