Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can think of futures and promises as two different sides of a pipe. On the promise side, data is pushed in, and on the future side, data can be pulled out.</p> <blockquote> <p>And future is some sort of an asynchronous operation that would complete in a different execution path.</p> </blockquote> <p>Actually, a future is a <em>placeholder object</em> for a value that may be become available at some point in time, asynchronously. It is not the asynchronous computation itself.</p> <p>The fact that there is a future constructor called <code>future</code> that returns such a placeholder object <em>and</em> spawns an asynchronous computation that completes this placeholder object does not mean that the asynchronous computation is called a <em>future</em>. There are also other <a href="http://www.scala-lang.org/api/current/index.html#scala.concurrent.Future$">future constructors/factory methods</a>.</p> <blockquote> <p>But the point I do not get is how promise has a future?</p> </blockquote> <p>To divide promises and futures into 2 separate interfaces was a design decision. You could have these two under the same interface <code>Future</code>, but that would then allow clients of futures to complete them instead of the intended completer of the future. This would cause unexpected errors, as there could be any number of contending completers.</p> <p>E.g. for the asynchronous computation spawned by the <code>future</code> construct, it would no longer be clear whether it has to complete the promise, or if the client will do it.</p> <p>Futures and promises are intended to constrain the flow of data in the program. The idea is to have a future client that subscribes to the data to act on it once the data arrives. The role of the promise client is to provide that data. Mixing these two roles can lead to programs that are harder to understand or reason about.</p> <p>You might also ask why the <code>Promise</code> trait does not extend <code>Future</code>. This is another design decision to discourage programmers from blindly passing <code>Promise</code>s to clients where they should upcast the <code>Promise</code> to <code>Future</code> (this upcast is prone to be left out, whereas having to explicitly call <code>future</code> on the promise ensures you call it every time). In other words, by returning a promise you are giving the right to complete it to somebody else, and by returning the future you are giving the right to subscribe to it.</p> <p>EDIT:</p> <p>If you would like to learn more about futures, Chapter 4 in the Learning Concurrent Programming in Scala book describes them in detail. Disclaimer: I'm the author of the book.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload