Resource.make
, it'll help you wrap things with a lifecycle
Guys, is there example how to work with cancelable IO
other than one in documentation? I cannot decipher it unfortunately. Here is what I want to achieve. I run synchronous DB query inside IO
. I want to let user of this IO cancel query on timeout. For that I need to call java.sql.Statement.cancel()
on statement that is used inside IO
. How can I do that ?
I looked at IO.runCancelable
and simply cannot understand how to use given SyncIO[CancelToken[IO]]
and how to link it with suspended action of parent IO
. Please advise.
IO
future.value
call that cats makes
future.onComplete
fromFuture
change, I'm just trying to rule out suspects
IO
? Here is how I do itval op: IO[String] = (1 to 100).foldLeft(IO.pure("")) { case (prev, count) =>
prev.flatMap{ _ =>
IO.sleep(500 millis) *> IO{
val msg = s"[${Thread.currentThread().getId}] Count: $count"
println(msg)
msg
}
}
}.bracketCase(value => IO.pure(value)){ case (str: String, value: ExitCase[Throwable]) =>
value match {
case ExitCase.Completed => IO(println("Completed"))
case ExitCase.Error(ex) => IO(println(s"Error: ${ex.getMessage}"))
case ExitCase.Canceled => IO(println("Canceled"))
}
}
println("Starting")
val fiberIO = op.start
fiberIO.unsafeRunAsyncAndForget()
Thread.sleep(2000)
println("Canceling")
fiberIO.flatMap(_.cancel).unsafeRunSync()
use
block ( value => ...
)