To speed up our code lets look at some ways to run promises in parallel. This can be useful when performing multiple database queries to render a web page and you want to run the queries in parallel rather than seqentially.
Date:
Words: 243
Time: 1 min read
Context
Multiple await calls will execute one after the other rather than in parallel. For example:
Here anotherCall() will be called only when someCall() is completed. What if you want to execute them in parallel?
Here's the simplest approach:
Note that Promise.all fails fast, which means that as soon as one of the promises supplied to it rejects, then the entire thing rejects. On the other hand, it will allow all the promises in it to run without blocking each other, but will prevent the execution to continue until ALL are resolved successfully.
For extra credit - here is a way to run many tasks in parallel and process the results individually as the tasks complete. I think it's pretty elegant.