Skip to content

5.1 Async Tasks and Callbacks

All operations in this library - like updating the screen or getting information about the scanner - are executed asynchronously.

The idea is to use Tasks. The result of the Task can be further processed once it is available. Within an async Task, other operations can be awaited for.

Warning

Task.await() is NOT available in Java because the Java Compiler does not support suspending functions.

Warning

Please be aware that Tasks rely on Kotlin Coroutines and therefore cannot be used for real blocking code. If you want to learn more about Coroutines, click here.

fun asyncTask() = Task<Unit> {
    // Wait for the Task result, which means it is blocking the current Task.
    veryLongRunningOperation().await()
}

// You can also add types to tasks
fun asyncTaskIntResult() = Task<Int> {
    delay(100) // wait 100ms

    return 1
}