Created
September 16, 2017 06:25
-
-
Save filosganga/62585b51b1d729210cf4cc9d341ffd86 to your computer and use it in GitHub Desktop.
How to convert a Guava ListeanbelFuture to Cats Async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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