We usually use flatmap and map in scala. Of course, if we use future we chaining by using flatmap or map… etc. But is this run parallel? In fact (as you know) it’s not parallel. If you chaining some monads, it runs sequentially.
Future chaining is not parallel
Let’s see some example. Guess that, there is some db dependent function like beneath
Each takes 2 seconds, 2 seconds, 3 seconds. And we want to make userInfo by calling functions. You can call like this.
As you can see, It takes 7 seconds (2 sec + 2 sec + 3 sec). It is not parallel.
How to be parallel?
There easy 2 ways to be parallel. Solve this by function. You can make like this.
It takes 5 seconds(2 seconds + max(2 seconds, 3 seconds)). It’s because, before chaining, Future already started. And after getting an email address, 2 seconds, we only need to wait for 1 second to get accountInfo because it runs parallel.
by using the Cartesian product and applicative
But it has to make function. It’s annoying. We can simply make this by using cats.
It also takes 5 seconds, But it is simpler than by using a function.