Hi all, so I hadn’t needed to do a lot of for comprehensions until I started using Kalix, and I have been doing calls to my entities in my controllers like this:
I’m thinking that even though the comprehension does indeed call each component sequentially, the call returns immediately with the future, so ‘sequential’ here doesn’t really mean much. Is that correct? or should I change my approach (which is fine just a more verbose and unwieldy).
You execute somemethod and when its result is ready, the someOtherMethod is executed.
For comprehension is just syntax sugar for a sequence of flatMaps. So in the end, they are effectively chained.
If the different calls are not dependent on each other, it’s indeed better to call them independently and not use the for comprehension only to collect the results.
In that example there is an ordering requirement, even if the second call does not contain anything from the response of the first call, it is not possible to add something to a cart before it has been successfully created, so they need to be sequential.
If the two calls were in parallel the addItem call could end up happening before create even though they were in the right order in the code, so it is not only about efficiency but also about ordering guarantees.
We could maybe also cover a separate scenario in the docs where the ordering does not make sense and two tasks can be executed in parallel with a single composed outcome once both are done though.