The Task
effect is a thin shim on top of Monix’s
Task
. This effect is not bundled in core Eff and requires
the eff-monix
extension to use.
Now, let’s create some Task
effects:
import org.atnos.eff._
import org.atnos.eff.addon.monix.task._
import org.atnos.eff.syntax.addon.monix.task._
import monix.eval.Task
type R = Fx.fx2[Task, Option]
val action: Eff[R, Int] =
for {
// create a value now
a <- Eff.pure[R, Int](1)
// evaluate a value later, on the thread pool specified by a Monix `Scheduler`, and continue when it's finished
b <- taskDelay[R, Int](1)
} yield b
Then we need to pass a Monix Scheduler
in to begin the
computation.
implicit val scheduler =
monix.execution.Scheduler(ExecutionContext.fromExecutorService(Executors.newScheduledThreadPool(10)): ExecutionContext)
Monix doesn’t natively offer an Await API to block on a Task result.
Instead it advises converting to a Scala Future
and using
Await.result
. See
https://monix.io/docs/3x/eval/task.html#blocking-for-a-result
import scala.concurrent.Await
Await.result(action.runOption.runAsync.runToFuture, 1.second)
> Some(1)