Clojure – await

Clojure await

In this guide, we will discuss Clojure await. Blocks the current thread (indefinitely!) until all actions dispatched thus far, from this thread or agent, to the agent(s) have occurred. Will block on failed agents.

Syntax

Following is the syntax.

(await agentname)

Parameters − ‘agentname’ is the agent for which the await function should be set to.

Return Value − None.

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)
   (await counter)
   (println @counter)
   
   (shutdown-agents))
(Example)

Output

The above program produces the following output.

0
100

You can see from the above program that the value of the agent is printed to the screen immediately because the await function will wait for Clojure to first update the value of the function and only then will return control to the calling program.

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply