import monix.catnap.syntax._
import scala.concurrent.Future
// Used here only for Future.apply as the ExecutionContextimport monix.execution.Scheduler.Implicits.global
// Can use any data type implementing Async or Concurrentimport cats.effect.IO
val io = IO(Future(1 + 1)).futureLift
IO provides its own IO.fromFuture of course, however
FutureLift is generic and works with
CancelableFuture as well.
import monix.execution.{CancelableFuture, Scheduler, FutureUtils}
import scala.concurrent.Promise
import scala.concurrent.duration._
import scala.util.Try
def delayed[A](event: => A)(implicit s: Scheduler): CancelableFuture[A] = {
val p = Promise[A]()
val c = s.scheduleOnce(1.second) { p.complete(Try(event)) }
CancelableFuture(p.future, c)
}
// The result will be cancelable:val sum: IO[Int] = IO(delayed(1 + 1)).futureLift
A type class for conversions from scala.concurrent.Future or other Future-like data type (e.g. Java's
CompletableFuture
).N.B. to use its syntax, you can import monix.catnap.syntax:
IO
provides its ownIO.fromFuture
of course, howeverFutureLift
is generic and works with CancelableFuture as well.