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.
IOis not only container of your listener. From cats-effect documentation:it means
IOcontains not the some value but intention to perform some side-effect.Here the most usual ways to work with
IO:IOusingflatMap(this way named composition):unsafeRunSyncor 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 executeIOto getA. You should use combination ofIOwhile 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
unsafeRunSyncmore than one time, and I guess, your signatureshould not use
IOAPI, but very similar toAsync[F[_]]API, take a look at functionasync:Try to concentrate that
listenshould do in your application and choose correct signature.Usefull links: