Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for a value of a Future

Tags:

scala

future

I want to check whether or not a Future f is completed right now by looking at a value its holding. That is not about

f onComplete {

}

It's about checking a value it's holding currently without blocking.

Your thoughts?

like image 673
Incerteza Avatar asked Jan 16 '26 21:01

Incerteza


2 Answers

f.value match {
  case Some(Success(x)) => println(s"the future is completed successfully with value $x")
  case Some(Failure(e)) => println(s"the future failed with an error: $e")
  case None => println("the future has not yet completed")
}
like image 163
stew Avatar answered Jan 19 '26 20:01

stew


You can use value if it is None the Future is not completed. If it is Some some will contain the value.

like image 32
Kocka Avatar answered Jan 19 '26 19:01

Kocka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!