January 2019
Intermediate to advanced
392 pages
10h 11m
English
A callback is a pattern that is used to retrieve the results of an asynchronous task. This approach assumes that we pass a reference to a function that should be invoked when an asynchronous operation is done.
The loadImage function in the following example code uses a callback to return the result:
fun loadImage(callback: (Image) -> Unit) { executor.submit { Thread.sleep(3000) callback(Image()) }}
The preceding code snippet shows the simplest example of how to create an asynchronous function that returns the results using the callback. In our case, the ...
Read now
Unlock full access