Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to convert a Guava ListeanbelFuture to Cats Async
import cats.effect.Async
import{FutureCallback, Futures, ListenableFuture}
import scala.concurrent.ExecutionContext
object GuavaFutures {
implicit class RichListenableFuture[T](lf: ListenableFuture[T]) {
def toAsync[F[_]: Async](implicit ec: ExecutionContext): F[T] = {
Async[F].async { cb =>
Futures.addCallback(lf, new FutureCallback[T] {
override def onFailure(t: Throwable): Unit = cb(Left(t))
override def onSuccess(result: T): Unit = cb(Right(result))
}, (command: Runnable) => ec.execute(command))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment