August 2018
Intermediate to advanced
380 pages
10h 2m
English
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 ...