async/await for Clojure
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 "https://gist.githubusercontent.com/divs1210/2ce84f3707b785a76d225d23f18c4904/raw/2dedab13201a8a8a2c91c3800040c84b70fef2e2/data.edn")
(defn fetch [url]
(promise [resolve]
(client/get url
{:async? true}
(fn [response]
(resolve [response nil]))
(fn [error]
(resolve [nil error])))))
(async
(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
CompletableFuture
complete
deref
ed: @(promise [resolve] (resolve :hi))
async
defn
and fn
forms - supports variadic versions(async (defn f
([a]
(inc a))
([a b]
(* a b))))
CompletableFuture
(promise
)@(async [1 2]) ;; => [1 2]
ExecutorService
using with-async-executor
await
async
let
bindings
let
block anywhere inside an async
blocklet
block with a call to await
returns a CompletableFuture
deref
: (await p 1000 :timeout)
channels
core.async
-like channel operations!Clone this repo, fire up a REPL, and walk through the conquerant walkthrough.
Copyright © 2018 Divyansh Prakash
Distributed under the Eclipse Public License either version 1.0.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close