0

Just try some simple Task examples. The following code works fine

import monix.eval.Task
import monix.execution.CancelableFuture
import monix.execution.Scheduler.Implicits.global

import scala.util.Success

val task = Task { 1 + 1 }
val cancellable = task.runAsync {
  case Right(result) => println(s"result is $result")
  case Left(err) => System.out.println(s"ERROR: ${err.getMessage}")
}

but using runToFuture works only in sandbox, not when i run it in intelliJ (of course in intelliJ i run it inside object)

val task = Task { 1 + 1 }

val future: CancelableFuture[Int] = task.runToFuture
future.onComplete {
  case Success(res) => println(s"result is: $res")
}

in intelliJ no printing 2, just

"C:\Program Files\Java\jdk1.8.0_192\bin\java.exe"

Process finished with exit code 0

What can be cause, i didn't expect stuck so early. Thanks in advance

Dmitry Reutov
  • 2,995
  • 1
  • 5
  • 20

1 Answers1

1

When run as a stand-alone program, the program exits before the task completes, so you don't get any output. You need to wait for the task to complete.

Await.result(future, Duration.Inf)
Tim
  • 26,753
  • 2
  • 16
  • 29
  • ok, i got, it works, but if i use delay it does not work even with Await. Do you know why? `val task = Task { 1 + 1 }.delayExecution(1.second)` – Dmitry Reutov Sep 28 '20 at 07:38
  • Sorry, I'm not familiar with Monix so I'm not sure why that is. – Tim Sep 28 '20 at 07:56