tldr; async-batch is a great way to manage complex task queues.
Async/await is a syntactic wrapper around Promises. So I will refer to Promises and Async/await interchangeably.
Below are several strategies with their pros and cons.
Strategy 1 - Simple For Loop
- Run promises sequentially, avoid potential concurrency issues
- Control load on external resources – ie. don't make too many queries at the same time
- Easy debugging / monitoring
- Slower if you want concurrent execution
Strategy 2 - Promise.all with Array.map
- Concise code that can run all functions concurrently
- Assumes the number of concurrent Promises is small and there are sufficient resources to run concurrently.
- Debugging becomes more complicated that a simple for loop if one of the promises fails
Strategy 3 - Async Batch
Async-Batch (GitHub, NPM) is an excellent dependency free library for managing concurrency in a set of promises. It allows you to specify the number of parallel promises running. As one completes, it starts the next in the queue.
Note: this library using ES6 syntax, so until Node.js fully supports it, you may need to use require().default.
- Efficiently manages large sets of async processes
- Relatively simple way to run complex task queues
- Dependency free
- Can scale the Parallelism from 1 to x dynamically, adjusting to available resources.
- Debugging is simpler than Promise.all since you can set parallelism to 1.
- ? Possibly more complex to read than the For loop.
Each method has its advantages, but I find myself using async-batch regularly for simple or complex scenarios.