leonoel / missionary

A functional effect and streaming system for Clojure/Script
Eclipse Public License 2.0
630 stars 26 forks source link

`timeout` usage is cumbersome #54

Closed leonoel closed 2 years ago

leonoel commented 2 years ago

When the target task fails to complete within given delay, the task fails and the error is not easy to check for recovery (in addition of not being documented). The timeout case could result in a success with a user-provided value, akin to the timed out version of deref. The timeout value could be optional and default to nil, like sleep.

(defn timeout
  ([task delay] (timeout task delay nil))
  ([task delay value]
   (-> task
     (attempt)
     (race (sleep delay #(-> value)))
     (absolve))))
(m/? (timeout (m/sleep 20 :a) 25 :b)) ;; :a after 20ms
(m/? (timeout (m/sleep 20 :a) 15 :b)) ;; :b after 15ms
(m/? (timeout (m/sleep 20 :a) 15))    ;; nil after 15ms
leonoel commented 2 years ago

Fixed in b.25