Combinators and event handlers

Let's say we would like to double the value in the age future. As we do with lists, we can simply map a function over the future to do just this:

(def double-age (i/map age #(* % 2))) 
;; #<Future@659684cb: #<Success@7ce85f87: 62>> 
While i/future schedules its body for execution on a separate thread, it's worth noting that future combinators such as map, filter, and so on, do not create a new thread immediately. Instead, they schedule a function to be executed asynchronously in the thread pool once the original future completes.

Another way to do something with the value of a future is to use the on-success event handler that gets called with the wrapped value of the future if it is successful:

(i/on-success ...

Get Hands-On Reactive Programming with Clojure - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.