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 async function Task.delay(Int msecs)

Returns a Task that becomes ready after msecs milliseconds.

static async function Task.idle()

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 async function Task.polish()

Returns a Task that is ready when the app has finished its next "polish" pass.

Certain operations, such as re-layouts caused by changing an Item's container, may be deferred until the polish pass. Awaiting on this task will allow you to wait until this is complete and the Item's state has been updated.

static async function Task.waitAll(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 async function Task.waitAny(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!")
    }
}