I need to make a variable amount of requests. Could be 2, could be 305. The server that the requests are made to runs on 10 threads, so it can handle 10 requests at a time. I predict that if I make all of them at the same time, the last ones will time out because the server is taking too long handling them all.
What I want to do is make them in batches of 10 to avoid this. However, because JS is async, a simple loop with 10 requests won’t work because they all will be made in one go anyway.
So what I’m searching for is a way to make a "group" of requests, wait until all of them have completed, and then continue on to the next group. I haven’t found a way to achieve this, so any help would be appreciated 🙂
2
Answers
What you’re looking for is a semaphore. There’s a package for javascript called async-mutex that can handle most of the setup you’ll need. You can set it up to allow a limited amount of processes access to a resource at once, which should let you batch your requests.
You can combine this with async functions to allow you to use promises if you need them to be synchronous.
Without code I don’t know what your implementation would look like, but you could do something like:
Which would limit you to 10 requests at a time, no matter how many are waiting in the queue.
Usually, you want to use resources most efficiently, and in this case it makes sense to not work in batches, but just limit number of active requests. So once request finishes new one is made. Blake’s answer describes how to achieve that using semaphore.
If you need to process data precisely in batches (i.e. wait for all requests from batch to complete before starting new one) you can use function like this: