I'm investigating Cats to accomplish the above. I tried writing the below example to
- Run an IO operation that doesn't block the main thread
- Throws an exception in the main thread if it fails
import cats.effect.{IO, Async}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
val apiCall = Future.successful("I come from the Future!")
val ioa: IO[String] =
Async[IO].async { cb =>
import scala.util.{Failure, Success}
Thread.sleep(1000)
throw new RuntimeException
apiCall.onComplete {
case Success(value) => cb(Right(value))
case Failure(error) => cb(Left(error))
}
}
ioa.unsafeRunAsync(result => result match {
case Left(result) => throw result
case Right(_) =>
})
However it seems to fail at both of those things. It blocks the main thread and only throws an exception in the child thread. Is what I'm trying to do possible?