Promise Pool is a concept to handle numerous concurrent operations more effectively in the progression of async functions.
Let's begin with a real-world use scenario.
Consider the scenario when we need to add a lot of products to the database from a CSV file or any other external API source.
The following code snippet represents one possible implementation.
However, a seasoned developer can make the observation and will say - "Hey dude, your code is inefficient because it waits for each import product process sequentially, thus overall product import process would take much more time." 👎
That's correct, so let's use the promise all feature to make it better so that each product import process can run concurrently and the whole import process will be incredibly quick.
But there are always drawbacks to advantages. 🤔
Now imagine that you are importing thousands of products. If you were to use
promise.all() in that scenario, you would end up running thousands of operations concurrently, which would result in a locking involved database tables, high CPU usage on the server, a backlog of requests for the V8 engine to process, as well as other problems.
Promise pools libraries can help in this situation. The idea is straightforward; it gives us the ability to manage multiple operations at once. The same principle is shared by many libraries, however in this article I'll focus on the Supercharge Promise Pool library.
withConcurrency API, we can define any positive arbitrary number, and it will carry out that many operations concurrently at any given time.
process function takes a callback function where any processing function can be passed and promises are resolved based on the defined concurrent operation capacity.
While both the Promise.all() and Promise Pool libraries can run several processes concurrently, the Promise Pool library is better if there is a larger amount of data to be processed because it has the advantage of providing API control over the parallel process.