I am using external API which offers:
def listen(listener: ResponseType => Unit): Handle
My listener returns:
IO[Unit]
Is there a way to inject my listener without a need to call unsafeRun?
listen(msg => myListener(msg).unsafeRunSync())
I am using external API which offers:
def listen(listener: ResponseType => Unit): Handle
My listener returns:
IO[Unit]
Is there a way to inject my listener without a need to call unsafeRun?
listen(msg => myListener(msg).unsafeRunSync())
Copyright © 2021 Jogjafile Inc.
IO
is not only container of your listener. From cats-effect documentation:it means
IO
contains not the some value but intention to perform some side-effect.Here the most usual ways to work with
IO
:IO
usingflatMap
(this way named composition):unsafeRunSync
or similar functions which runIO
. It should be called once in your application and as a rule in the end of the universe (at the end of your program).So when you see
IO[A]
you should remember that it is not the similar thing asOption[A]
orList[A]
and you can just executeIO
to getA
. You should use combination ofIO
while your application is not on the end of execution. If so you can run it usingunsafeRunSync
.I mean you should build your application without using
unsafeRunSync
more than one time, and I guess, your signatureshould not use
IO
API, but very similar toAsync[F[_]]
API, take a look at functionasync
:Try to concentrate that
listen
should do in your application and choose correct signature.Usefull links: