Execution Paradigms & The Power of Promise.all
Today I will talk about the differences between sequential, concurrent, and parallel executions and why it is very beneficial to use Promise.all method in JavaScript.
If you understand this, you can take advantage of the application performance benefits of using Promise.all in your applications.
Many people execute their methods in the most common ways without taking advantage of JavaScript's full potential.
Sequential vs Concurrent vs Parallel
Let's define each of the types of executions:
- Sequential: the processing is carried out and waits to finish to complete the following.
- Concurrent: the execution of processing is triggered, and before it ends, another is executed. The two remain resolved in the background. It is essential to see that one is executed first and then another, not at the same time.
- Parallel: both processes are executed at the same time.
Sequential
We have two asynchronous methods:
getUser() is not executed until getInfo() is executed and finished.
Concurrent
To see how to run processes concurrently in JavaScript, you have to use Promise.all method.
getInfo() is executed, and without waiting for it to resolve and finish, getUser() is executed afterwards. The program ends when both asynchronous processes are resolved.
Parallel
Parallel execution means that both functions would have to be executed simultaneously. That is not possible in JavaScript since it is a single-threaded language.
Conclusion
By using Promise.all you can take advantage of the potential of concurrent execution in JavaScript and improve the performance of your applications since you can start processing multiple executions without having to wait for them to finish.
Here, I share a helpful resource that also talks about this topic. I strongly recommend it:
I hope you enjoyed the article.
Join my weekly newsletter, where I share articles to help you become a better front-end developer. You'll receive them directly to your inbox.
If you have any questions, feel free to contact me on LinkedIn, Edi Rodriguez, or leave a comment in the post.
See you in the next post.
Have a great day!