liveBook · Manning

Deref

As explained in Clojure: The Essential Reference

deref is a Clojure function that retrieves the current or available value from a dereferenceable object, optionally with a timeout and default value.

deref retrieves a value from a dereferenceable object. Use it to read current state, obtain asynchronous results, force delays, or wait for promises; the @ reader macro is shorthand. section 14.0 section 14.7

Its behavior depends on the value being dereferenced:

  • Returns current state for an agent, atom, var, or volatile!, and returns the appropriate transactional value for a ref. section 14.7
  • Forces a delay once, then returns its cached value; for a future, it may block until evaluation finishes. section 14.7
  • Blocks on a promise until a value is delivered. Use the timeout arity with a default value when waiting indefinitely is unsafe. section 14.2 section 14.7
  • Use realized? when you need a nonblocking true-or-false check rather than the value itself. section 14.7

Waiting for asynchronous work

(def sum (future (Thread/sleep 10000) (+ 1 1))) (realized? sum) ; false (deref sum) ; 2 (realized? sum) ; true A timeout supplies a fallback instead of waiting forever: (deref p 100 "fail!")

How Clojure: The Essential Reference covers it

How deep it goes
Defines type-specific behavior, blocking, timeouts, reader syntax, and nonblocking realization checks.
Examples
Demonstrates dereferencing futures and promises, timeout fallbacks, asynchronous RSS aggregation, and promise-driven concurrency.
What it assumes
Assumes basic Clojure expressions and concurrency values while teaching reference-specific blocking behavior.
What you can do after
Apply it via Clojure to read reference state, await asynchronous results, or avoid indefinite waits with timeout defaults.
How it connects
Connects futures, promises, delays, agents, atoms, refs, vars, volatile!, reduced, and realized?.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.