dnowak
04/14/2020, 10:03 PMIO.unsafeRunSync()
so dangerous that it cannot be called in production code? I need to execute IO
in the current thread and unsafeRunSync
seems reasonable in such case. Questions:
1. What can go wrong during the execution of unsafeRunSync
?
2. How to execute IO
in the current thread?pakoito
04/14/2020, 11:44 PM.attempt().unsafeRunSync()
runs your program as returns an Either
. It’s meant as an entry point. If you need to do IO
inside something that’s not entry-programmy like an event callback, there’s a way of doing it through a Promise
or a Channel
.pakoito
04/14/2020, 11:47 PMpakoito
04/14/2020, 11:50 PMsystem.handleEvent {
val bla = run(it).unsafeRunSync() // block thread
continue(bla) // side-effect outside IO
}
you have
IO.fx {
continueOn(dispatchers().default()) // process on the background
val next = queue.take().bind()
val bla = run(it).bind()
effect { continue(bla) }.bind()
}.unsafeRunAsync { } // immediately free the thread
system.handleEvent {
queue.offer(it)
.fork() // offer the value on the background
.unsafeRunAsync { } // immediately free the thread
}
pakoito
04/14/2020, 11:55 PMmyIO.attempt().suspended()
is better than unsafeRunAsync()