Asynchronous Clojure

Getting started with channels and the core.async library

Functional Human
The Startup

--

Core.async is Clojure’s main library for asynchronous programming and communication. It makes asynchronous programming a lot easier by doing much of the heavy lifting for you.The library has a few objectives which you can read about here, but chiefly it aims to:

  • To provide facilities for independent threads of activity, communicating via queue-like channels
  • To support both real threads and shared use of thread pools (in any combination), as well as ClojureScript on JS engines
Photo by nima hatami on Unsplash

Channels

The basic building block is the Channel. Core.async provides a number of channel types off the shelf; buffered channels, dropping-buffer channels and sliding-buffers.

You can think of a channel as a conveyer belt of items that you can add at one end of the conveyer belt and take from the other end. You can only take one item at a time and occasionally you might have to queue up outside of the conveyer belt if there are other people adding items on in front of you. Sometimes you might have to also queue up to collect your items too. The belt can vary in size as well. Small conveyer belts can result in large queues building up outside, especially once the belt becomes full and there are no more people to…

--

--