Clojure - await-for



Since there is a delay when a value of an agent is updated, Clojure provided a ‘await-for’ function which is used to specify time in milliseconds to wait for the agent to be updated.

Syntax

Following is the syntax.

(await-for time agentname)

Parameters − ‘agentname’ is the agent for which the ‘await-for’ function should be set to. ‘time’ is the time in milliseconds to wait.

Return Value − Returns logical false if returning due to timeout, otherwise returns logical true.

Example

An example on how this is used is shown in the following program.

(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def counter (agent 0))
   (println @counter)
   
   (send-off counter + 100)
   (println (await-for 100 counter))
   (println @counter)
   
   (shutdown-agents))
(Example)

Output

The above program produces the following output.

0
true
100

You can see from the above program that the value of the agent is printed to the screen immediately because the ‘await-for’ function incorporated a delay, which allowed Clojure to update the value of the agent.

clojure_agents
Advertisements