Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating an empty Scala Future to be completed externally

Is it possible / how to create an empty Future, set the necessary callbacks for its completion, and then set its result at a different point in time? In pseudocode (uncompilable) that would look like the following:

val future: Future[Int] = Future.empty()
future.onComplete {
  case Failure(exception) => 
    println(s"Exception: $exception")
  case Success(value) =>
    println(s"Done: $value")
}
 
// from a different thread at some point in the future
future.set(123)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are looking for the Promise.

val promise = Promise[Int]()
val future = promise.future
future.onComplete { ... } 

promise.success(123)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading