Use of promises


#1

Hello,
I am new to Clojure and I was having a look at the implementation of Lacinia (GraphQL implementation in Clojure) when I stumbled upon the following function:

(defn execute-parsed-query
  "Prepares a query, by applying query variables to it, resulting in a prepared
  query which is then executed.

  Returns a result map (with :data and/or :errors keys)."
  [parsed-query variables context]
  (let [result-promise (promise)
        execution-result (execute-parsed-query-async parsed-query variables context)]
    (resolve/on-deliver! execution-result
                         (fn [result]
                           (deliver result-promise result)))
    ;; Block on that deliver, then return the final result.
    @result-promise))

In this function they create a promise object and immediately try to dereference it.
So why use a promise at all when the thread immediately blocks anyway?
Am I missing something?

Thank you.


#2

Not sure but it may be because resolve/on-deliver! happens to have an async, callback-based API. So this function turns the async function into a blocking call.


#3