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!")
