Imminent – a composable futures library for Clojure

The author of the first edition of this book, Leonardo Borges, created an open source library that aims to fix the previous issues with Clojure futures. The result of this work is called imminent (see https://github.com/leonardoborges/imminent).

The fundamental difference is that imminent futures are asynchronous by default and provide a number of combinators that allow us to declaratively write our programs without us having to worry about its order of execution.

The best way to demonstrate how the library works is to rewrite the previous movies example in it. We will do this in two broad steps.

First, we will individually examine the bits of imminent's API that will be part of our final solution. Then, we'll put it all together in a working application.

Let's start by creating a new project:

    lein new imminent-playground  

Next, add a dependency on imminent to your project.clj:

:dependencies [[org.clojure/clojure "1.9.0"] 
               [com.leonardoborges/imminent "0.1.1"]] 

Then, create a new file, src/imminent_playground/repl.clj, and add imminent's core namespace:

(ns imminent-playground.repl 
  (:require [imminent.core :as i])) 
 
(def  repl-out *out*) 
(defn prn-to-repl [& args] 
  (binding [*out* repl-out] 
    (apply prn args))) 

The preceding snippet also creates a helper function that comes in useful when we're dealing with multiple threads in the REPL. This will be explained in detail later, but for now, just take this as being a reliable way to print to the REPL across multiple threads.

Feel free to type this in the REPL as we go along. Otherwise, you can acquire the namespace file from a running REPL, like so:

    (require 'imminent-playground.repl)  

All of the examples in this chapter will be in this file.