DEV Community

Cover image for Run multiple tasks Concurrently (in parallel) using JavaScript
King Elisha
King Elisha

Posted on

Run multiple tasks Concurrently (in parallel) using JavaScript

When you have multiple time-consuming tasks/functions to execute, there are two main solutions to optimize the execution time and speed up your app:

Run everything at once with Promise.all()

If your functions are promise-based, they can easily be executed concurrently using Promise.all()

Functions that work with properly formatted call-backs β€” where the first argument of the call-back is reserved for errors and the second argument is the value to be returned β€” can easily be promisified using the promisify utility function and executed concurrently.

Run a fixed batch concurrently

If your functions require significant resources to execute, running them all at once with Promise.all() may cause your application to crash. A solution to this is to create a TaskQueue that can execute a fixed number of tasks concurrently

The runTask method executes each batch concurrently and resolves with the results of all the functions after executing all batches. This way, the speed of execution is improved without going overboard on computing resources.

Thanks πŸ‘ for making it to the end πŸ‘¨β€πŸ’» and I really hope you found the content useful.

Top comments (0)