You are using the Promise
wrong.
a.success
method completes the promise with given argument, it doesn't run the expression you pass to it asynchronously.
What you probably want to do is something like this:
val f = Future(burncpu(6000))
Assuming you have an ExecutionContext
available (if you don't, you can do import ExecutionContext.Implicits.global
), this will construct a Future
which will run your function asynchronously.
You can see how it works in Scala REPL (f.value
returns None
until the method has returned)
scala> val f = Future(burncpu(6000))
f: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise@4d4d8fcf
scala> f.value
res27: Option[scala.util.Try[Int]] = None
scala> f.value
res28: Option[scala.util.Try[Int]] = None
scala> f.value
res29: Option[scala.util.Try[Int]] = Some(Success(6000))