Skip to Content
Mastering Functional Programming
book

Mastering Functional Programming

by Anatolii Kmetiuk
August 2018
Intermediate to advanced
380 pages
10h 2m
English
Packt Publishing
Content preview from Mastering Functional Programming

Asynchronous example

We can redefine our previous example in terms of async as follows:

def async(name: String): IO[Int] =  IO.async { cb =>    new Thread(new Runnable { override def run =      cb { Right(taskHeavy(name)) } }).start()  }

So, here, we are using the IO.async primitive to lift our computation into an asynchronous context. First of all, this async method gives us a callback as an input. We are supposed to call this callback once we are done with our computation.

Next, we dispatch our heavy computation to some other execution context. In our case, it is merely starting another thread that does not belong to the thread pool on which we are executing our IO. Many scenarios are possible here, especially in the context of purely asynchronous ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781788620796Supplemental Content