Do we have an Await

150 views
Skip to first unread message

cees van Kemenade

unread,
Aug 30, 2018, 6:03:38 AM8/30/18
to clojure-clr

When using C# Async methods  you also would like to have the Clojure-clr equivalent of the await.
Does anyone know whether this exists in clojure-clr?

I've made an await function myself, but possbily a better and more idiomatic solution is available.

I currently use:


(def WaitInterval (Convert/ChangeType 30 System.Int32))

(def RunStatus #{System.Threading.Tasks.TaskStatus/Running
                 System.Threading.Tasks.TaskStatus/WaitingForActivation})
(defn C#await
  [task]
  (while (RunStatus  (.Status task))
      (System.Threading.Thread/Sleep ^System.Int64 WaitInterval))
  (when (not= (.Status task) System.Threading.Tasks.TaskStatus/RanToCompletion)
    (println "Request has unexpected status: " (.Status task)))
  (.Result task))



ad...@tothadam.net

unread,
Oct 11, 2018, 7:57:58 AM10/11/18
to clojure-clr
This implementation blocks the thread it is running on, you can probably just use .Result on the task to achieve the same. It will also return faster and spin less.
Ideally, you could use clojure idioms with core.async instead of using await, however it is not fully implemented yet for ClojureCLR: https://github.com/dmiller/clr.core.async
Not sure if there is a way to await in ClojureCLR.
Reply all
Reply to author
Forward
0 new messages