async/await for Clojure

async-await, clojure, concurrency, library, promise



async/await for Clojure

Clojars Project Build Status codecov

A lightweight Clojure wrapper around ForkJoinPool and CompletableFuture for concurrency that is simple and easy.


core.async is very powerful, but quite low-level by design


;; Async HTTP Exaxmple
;; ===================
(refer-clojure :exclude '[await promise])
(require '[clj-http.client :as client]
         '[conquerant.core :refer [async await promise]])

(def url "")

(defn fetch [url]
  (promise [resolve]
    (client/get url
                {:async? true}
                (fn [response]
                  (resolve [response nil]))
                (fn [error]
                  (resolve [nil error])))))

  (let [[response error] (await (fetch url))]
    (if error
      (println "Error:" (.getMessage error))
      (println "Response Body:" (:body response)))))

(println "fetching asynchronously...")
;; => fetching asynchronously...
;; => Response Body: {:result 1}
  • promise

    • gets value/error out of callback
    • returns a CompletableFuture
  • async

    • can wrap
      • defn and fn forms - supports variadic versions
      (async (defn f
                 (inc a))
               ([a b]
                 (* a b))))
      • any other expression, returning a CompletableFuture
      @(async [1 2]) ;; => [1 2]
    • conquerant.internals/*executor* is bound to the common ForkJoinPool pool by default
  • await

    • can only be used in async let blocks
      • normal let block anywhere inside an async block
      • every let block with a call to await returns a CallableFuture
      • works across function boundaries


Copyright © 2018 Divyansh Prakash

Distributed under the Eclipse Public License either version 1.0.