Public Vars

Back

future (clj)

(source)

macro

(future & body)
Takes a body of expressions and yields a future object that will invoke the body in another thread, and will cache the result and return it on all subsequent calls to deref/@. If the computation has not yet finished, calls to deref/@ will block, unless the variant of deref with timeout is used. See also - realized?.

Examples

babashka/babashka
(ns babashka.impl.protocols
  (:require [babashka.impl.protocols :as protocols]
            [clojure.core.protocols :as p]
            [clojure.datafy :as d]
            ;; ensure datafy is loaded, we're going to override its
            ;; clojure.lang.Namespace implementation for datafy
            [clojure.reflect]
            [sci.core :as sci :refer [copy-var]]
            [sci.impl.types :as types]
            [sci.impl.vars]))

   ;; IKVReduce only added for satisies? check for now. We can implement
   ;; kv-reduce in the future, but this needs patching some functions like
   ;; update-vals, etc.
   'IKVReduce (sci/new-var 'clojure.core.protocols/IKVReduce {:protocol p/IKVReduce
                                                              ;; :methods #{'kv-reduce}
                                                              :ns protocols-ns}
                           {:ns protocols-ns})
   ;; 'kv-reduce (copy-var kv-reduce protocols-ns)
   }
  )
clojure/core.async
;; The clojure.core.async namespace contains the public API.
(require '[clojure.core.async :as async :refer :all])

;; Because these are blocking calls, if we try to put on an
;; unbuffered channel, we will block the main thread. We can use
;; `thread` (like `future`) to execute a body in a pool thread and
;; return a channel with the result. Here we launch a background task
;; to put "hello" on a channel, then read that value in the current thread.
clojure/core.typed
(ns ^:skip-wiki clojure.core.typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  (:require [#?(:clj clojure.core.typed
                :cljs cljs.core.typed)
             :refer [defalias] :as t]))

(defalias
  ^{:doc "A Clojure future (see clojure.core/{future-call,future})."
    :forms '[(Future t)]}
  t/Future 
  (t/TFn [[x :variance :covariant]]
         (t/I (t/Deref x)
              (clojure.lang.IBlockingDeref x)
              clojure.lang.IPending
              java.util.concurrent.Future)))
metosin/compojure-api
(ns example.handler
  "Asynchronous compojure-api application."
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]
            [manifold.deferred :as d]
            [clojure.core.async :as async]
            compojure.api.async))

     (GET "/minus" []
       :return {:result Long}
       :query-params [x :- Long, y :- Long]
       :summary "subtract two numbers from each other"
       (fn [_ respond _]
         (future
           (respond (ok {:result (- x y)})))
         nil))

   (context "/resource" []
     (resource
      {:responses {200 {:schema {:total Long}}}
       :handler (fn [_ respond _]
                  (future
                    (respond (ok {:total 42})))
                  nil)}))))
puppetlabs/trapperkeeper
(ns puppetlabs.trapperkeeper.app
  (:require [schema.core :as schema]
            [puppetlabs.trapperkeeper.services :as s]
            [clojure.core.async.impl.protocols :as async-prot])
  (:import (clojure.lang IDeref)))

(def TrapperkeeperAppContext
  "Schema for a Trapperkeeper application's internal context.  NOTE: this schema
  is intended for internal use by TK and may be subject to minor changes in future
  releases."
  {:service-contexts {schema/Keyword {schema/Any schema/Any}}
   :ordered-services TrapperkeeperAppOrderedServices
   :services-by-id {schema/Keyword (schema/protocol s/Service)}
   :lifecycle-channel (schema/protocol async-prot/Channel)
   :shutdown-channel (schema/protocol async-prot/Channel)
   :lifecycle-worker (schema/protocol async-prot/Channel)
   :shutdown-reason-promise IDeref})