Task

class Task<T>

Asynchronous Task

When interacting with the scanner, we usually don't want to block the main thread Instead we want to e.g. update the UI and use the result once it is available

Since we are supporting multiple platforms, we have to use Kotlin coroutines, which support every platform to some extent. The only function that is not available on JS is runBlocking, that's why this Task does not support being compiled to JS

Getting the result

The first way of receiving the result of a task is by using await. This will block the coroutine execution until the result is available or an exception is thrown. This is only available in a Coroutine Context.

The second way is by using onComplete, onSuccess or onError. By using these methods, we are not blocking and instead are executing the task asynchronously Once a result is received, the result can be used in the closure

Constructors

Link copied to clipboard
constructor(start: CoroutineStart = CoroutineStart.DEFAULT, runnable: suspend (Task<T>) -> T)
constructor(promise: Deferred<T>)

Properties

Link copied to clipboard

defines whether task has completed. Completed does not mean that it was successful, only that there is no work to do anymore.

Link copied to clipboard
val result: T

The result of the task. Should only be called once Task finished indicated by isComplete. Use onComplete, waitForResult or await to ensure that.

Link copied to clipboard
val scope: <Error class: unknown class>

Functions

Link copied to clipboard
suspend fun await(): T

waits for finishing the task within a suspending function. returns result or throws exception if exception occurred

Link copied to clipboard
fun onComplete(taskCallback: TaskCallback<T>): Task<T>
fun onComplete(completeCallback: (param: Task<T>) -> Unit): Task<T>

Lambda is called once task has completed. You can then check .result for successful result or it will throw an error. Returns Task.

Link copied to clipboard
fun onError(errorCallback: (err: Throwable) -> Unit): Task<T>

Lambda is called once error was thrown. Returns Task.

Link copied to clipboard
fun onSuccess(doneCallback: (param: T) -> Unit): Task<T>

Lambda is called once task was successful. Won't be called for errors. Returns Task.

Link copied to clipboard
suspend fun waitForResult(): <Error class: unknown class>

Waits for the result of the current task, without caring about the result or an error. It won't return the result neither will it throw. You have to call Task.result for this.