How do Clojure futures and promises differ?

后端 未结 5 1773
感情败类
感情败类 2020-12-22 22:12

Both futures and promises block until they have calculated their values, so what is the difference between them?

5条回答
  •  既然无缘
    2020-12-22 22:57

    Both Future and Promise are mechanisms to communicate result of asynchronous computation from Producer to Consumer(s).

    In case of Future the computation is defined at the time of Future creation and async execution begins "ASAP". It also "knows" how to spawn an asynchronous computation.

    In case of Promise the computation, its start time and [possible] asynchronous invocation are decoupled from the delivery mechanism. When computation result is available Producer must call deliver explicitly, which also means that Producer controls when result becomes available.

    For Promises Clojure makes a design mistake by using the same object (result of promise call) to both produce (deliver) and consume (deref) the result of computation. These are two very distinct capabilities and should be treated as such.

提交回复
热议问题