February 2018
Intermediate to advanced
350 pages
7h 35m
English
A popular style of asynchronous code is to execute the code in a separate thread and invoke a callback function when the aforementioned thread finishes its execution. One downside of the callback style is that our asynchronous functions now need an extra parameter. Callback style is easy to write in Kotlin with its support for lambdas.
For our callback implementation, we'll need adapters for our clients and repositories:
import kotlin.concurrent.threadclass CallbackUserClient(private val client: UserClient) { fun getUser(id: Int, callback: (User) -> Unit) { thread { callback(client.getUser(id)) } }}class CallbackFactClient(private val client: FactClient) { fun get(user: User, callback: (Fact) -> Unit) { thread { callback(client ...
Read now
Unlock full access