Skip to main content
Version: 2.0.0-beta14

FromFuture

FromFuture

FromFuture is a typeclass to convert scala.concurrent.Future to an effect, F[_]. So if there are some APIs returning Future, it can be converted to F[_].

There are three FromFuture instances available.

  • FromFuture for monix.eval.Task
  • FromFuture for scala.concurrent.Future
  • FromFuture for cats.Id
trait FromFuture[F[_]] {
def toEffect[A](future: => Future[A]): F[A]
}

FromFuture.toEffect

import cats._
import cats.syntax.all._
import monix.eval._

import effectie.core._
import effectie.syntax.all._

import extras.concurrent.ExecutorServiceOps

import java.util.concurrent.{ExecutorService, Executors}
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._

object MyApp {

def foo(n: Int)(implicit ec: ExecutionContext): Future[Int] =
Future(n + 100)

def bar[F[_]: Fx](n: Int): F[Int] =
pureOf(n * 2)

def baz[F[_]: Monad: Fx: FromFuture](n: Int)(implicit ec: ExecutionContext): F[Int] =
for {
a <- FromFuture[F].toEffect(foo(n))
b <- bar[F](a)
} yield b

}

val executorService: ExecutorService =
Executors.newWorkStealingPool(Runtime.getRuntime.availableProcessors() >> 1)
// executorService: ExecutorService = java.util.concurrent.ForkJoinPool@2e294894[Terminated, parallelism = 2, size = 0, active = 0, running = 0, steals = 0, tasks = 0, submissions = 0]
implicit val ec: ExecutionContext = ExecutionContext.fromExecutorService(executorService)
// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$4@776931a2

import monix.execution.Scheduler.Implicits.global
try {
import effectie.instances.monix3.fx._
import effectie.instances.monix3.fromFuture._
println(MyApp.baz[Task](1).runSyncUnsafe())
} finally {
ExecutorServiceOps.shutdownAndAwaitTermination(executorService, 1.second)
}
// 202