How do Clojure futures and promises differ?

后端 未结 5 1772
感情败类
感情败类 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:49

    In Clojure, promise, future, and delay are promise-like objects. They all represent a computation that clients can await by using deref (or @). Clients reuse the result, so that the computation is not run several times.

    They differ in the way the computation is performed:

    • future will start the computation in a different worker thread. deref will block until the result is ready.

    • delay will perform the computation lazily, when the first client uses deref, or force.

    • promise offers most flexibility, as its result is delivered in any custom way by using deliver. You use it when neither future or delay match your use case.

提交回复
热议问题