Reading values from a Future

As the Future is not implemented as an ADT, we can't directly pattern-match on it as we did with other the effects that we looked at in this chapter.

Instead, we can use the null-checking style:

scala> if (runningLong.isCompleted) runningLong.value
res54: Any = Some(Success(()))

Luckily, the value method returns an Option that will be None until the future completes, so we can use this in a pattern match:

scala> runningForever.value match {
| case Some(Success(value)) => println(s"Finished successfully with $value")
| case Some(Failure(exception)) => println(s"Failed with $exception")
| case None => println("Still running")
| }
Still running

Of course, the most useful methods are defined not in relation to the value of the Future, but in terms of Future as an effect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.212