I'm trying to get my head around this concept. I clearly understand what Futures are. I'm little bit bemused with Promises. The code snippet below:
val p = Promise[Int]() // gives me a DefaultPromise
val f = p.future // gives me the future computation for the Promise p
Now what is the difference between the following two code snippets?
p success { 10 }
and
val x = Future {
p success { 10 }
}
My understanding of the first one is that the p success will complete the future computation associated with that p. Is that computation asynchronous? How is that different to the second code snippet that uses a Future block to complete the Future f associated with Promise p?
p success{10}
andFuture(10)
would be equivalent. SoFuture{ p success { 10 }.future }
would be likeFuture(Future(10))
. In few words, you are composing a Promise into a Future. There is a good description here. You can think about Promises like a Future you can write in. When you definep success {x}
you are saying that you are going to build a Future that is going to be successful and will return the value x. – Carlos Vilchezp failure trowable
. – Carlos Vilchez