3/29/2018 - 6:18 AM

promise and deliver (also future)

promise and deliver (also future)

;; shipping products for Order
;; 组装产品等待其他工序完成才可以完成.

(defrecord Order [name price qty])

;; 统计总价
(defn merge-products [m1 m2]
  ;; meta data info `total-price`
  {:total-price (+ (* (.price x) (.qty x)) (* (.price y) (.qty y)))}
  [m1 m2])

;; 组装完产品后发货, 等待x,y完毕,即x和y的deliver
(defn ship-products [x y z]
  (deliver z (merge-products @x @y))
  (println "We can ship products " @z))

(def product-a (promise))

(def product-b (promise))

(def shipping-ab (promise))

(future (ship-products product-a product-b shipping-ab))

(deliver product-a (->Order "book" 10.1 5))

(deliver product-b (->Order "pencil" 2.1 10))