Case 1: List of tasks

Let’s say you have a list of posts that need to be published on an external service. Before publishing you want to validate some post fields and if they’re OK to publish.

The sequential solution 😴

You can iterate over the list and check validation for each post, then if the post is valid proceed and publish it.

This solution works, but you need to pay attention that each post has to wait until the end of the previous one to finish.

There is no reason why we should not execute all of them simultaneously.

The concurrent solution 😎

First we will create a publishing pipeline: validate → publish

Second, we will map all our posts to the pipeline and get back a list of tasks (Promises). Now we can call “Promise.all” to wait until all of the pipelines are done. Execution time has been reduced to almost one pipeline because each pipeline is running independently from one other.

If you are dealing with very long sequences of posts it’s a good idea to split the pipelines into chunks and execute them one by one.