Skip to content

Instantly share code, notes, and snippets.

@filosganga
Created September 16, 2017 06:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filosganga/62585b51b1d729210cf4cc9d341ffd86 to your computer and use it in GitHub Desktop.
Save filosganga/62585b51b1d729210cf4cc9d341ffd86 to your computer and use it in GitHub Desktop.
How to convert a Guava ListeanbelFuture to Cats Async
import cats.effect.Async
import com.google.common.util.concurrent.{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