Task

Task is a generic value type that represents an ongoing operation.

Task is the cornerstone of Zing's asynchronous API framework.

property Bool isReadyread only

True if the task is ready and the result value, if applicable, is available.

Awaiting on a ready task will continue immediately.

property type valueread only

The task's result value. Until the task is ready, the value property will return the default value for the result type.

For the unparameterized Task type, the value property is absent.

Task voidTask
voidTask.value // Error

Task<Int> intTask
intTask.value // 0

static function Task.delay(Int msecs) → Task

Returns a Task that becomes ready after msecs milliseconds.

static function Task.idle() → Task

Returns a Task that is ready when the app is idle. The idle task can be used to split a time consuming workload across multiple frames without causing the app to visibly stutter.

static function Task.waitAll(Task task, Task task...) → Task

Returns a Task that becomes ready once all the argument tasks are ready.

If all the passed tasks are already ready, the returned task will also be ready.

async function go() {
    var bing = Http.get("http://www.bing.com")
    var google = Http.get("http://www.google.com")

    await Task.waitAll(bing, google)

    System.log("Bing is " + bing.value.body.length + " bytes")
    System.log("Google is " + bing.value.body.length + " bytes")
}

static function Task.waitAny(Task task, Task task...) → Task

Returns a Task that becomes ready once at least one of the parameter tasks are ready.

If any one of the passed tasks is already ready, the returned task will also be ready.

async function go() {
    var bing = Http.get("http://www.bing.com")
    var google = Http.get("http://www.google.com")

    await Task.waitAny(bing, google)

    if (bing.isReady) {
        System.log("Bing was fastest!")
    } else {
        System.log("Google was fastest!")
    }
}