Also, most web apps today brings side effects from...
# arrow
s
Also, most web apps today brings side effects from the very bottom. Like repositories (http call or db call), so most of the functions would have returning type as IO<A>. That being said, do I gain anything except for
direct imperative syntax
(which i am not leveraging here) from a function with returning type IO if I execute my IOs in fx ?
for example :
Copy code
import <http://arrow.fx.IO|arrow.fx.IO>
import arrow.fx.extensions.fx

fun someFun(): IO<Unit> = IO { println("hey") }
fun anotherFun(): IO<Unit> = IO { println("hey again") }

fun main() {
    someFun().followedBy(anotherFun()).unsafeRunSync()
}

fun mainMain() {
    IO
        .fx {
           someFun().followedBy(anotherFun()).bind()
        }
        .unsafeRunSync()

}

main()
mainMain()
Use IO only if suspend isn’t enough. Wrap suspend functions with
effect
. Parallelize with
parMapN
. Race IOs with
raceN
. Change threads with
continueOn
. Await IOs with prefix
!
inside
fx
, and
suspended
inside a suspend function
and then
There are other classes to complement IO such as Promise (hot, single-result), Timer (sleep), Ref (atomic reference), MVar (concurrent queue size 1), Resource (acquire/release closeables), Semaphore (concurrent access to resource). They all use IO’s race/parallelism/cancelation.
basically
someFun
should be
suspend
unless you have to do any of the things above
and if you have a function that does those, i.e. run
someFun
in another thread, then you use IO
same for kotlinx.coroutines, you only use launch, async, withContext and the rest of the family when suspend isn’t enough
s
aaaahhhhhh… Thank you so much 🙂 this clears so many clouds for me.