Question: How to async with two independent Aff/TryAsync
See original GitHub issueHi,
I have a little question about the best way handling two different async Monads with side effects, like Aff and TryAsync. The best thing about async is that it frees the thread for work while an asynchronous operation is waiting for a result of an external source. So sometimes, when two tasks are indepentend of each other, it allows us to run them kind of parallel. In the example below we have to start two different http requests which do not need the result of the other, so we can start the second before the first one has finished
var request1Task = Request1();
var request2Task = Request2();
await request1Task;
await request2Task;
But things get tricky (for me) when it comes to the effect monads. In the following example I will return Aff instead of raw Tasks, but the only way I found to run the execution like the one above is with calling Run on both and awaiting the Fin.
var request1Running = Request1().Run();
var request2Running = Request2().Run();
var fin1 = await request1Running;
var fin2 = await request2Running;
However, from my understanding of the docs the effects should be composed and only be run once at the start of the application, which is broken by this code style. I understand that this code is not executed directly when wrapped in another Aff itself. That way the rule can be followed but I don’t know if that’s how it’s supposed to be. So am I missing another way to achieve the effect of the code in the example above?
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)

Top Related StackOverflow Question
With
Affyou can call:With up to 5 items in the tuple (which will all run in parallel, and only return when all are complete).
TryAsyncdoesn’t have this behaviour. I’d encourage you to useAffas it’s going to get the lion share of the effort going forward.What more important IMHO is e.g. if you have an Enumerable<Task<Sideeffects (external API-Call)>>. If you take e.g. an Enumerable of 1.000.000 of those Tasks and do a .Select or .ForEach, there is a big chance that you crash the external API or the network or even both. With TraverseParallel with the window of parallelity you can backpressure the amount of simultaneous call to external.
(It´s like groupBy(count) and foldLeft in scala, if you don´t want to use fs2 or akka.streams).
Btw. Thanks for that outstanding framework!