The same scala Task code works in sandbox but doesn't work in intelliJ

87 views Asked by At

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

1

There are 1 answers

2
Tim On BEST ANSWER

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)