API-Version: 1.9

Deferred

Deferred objects can be used to manage asynchronous operations. It can trigger a callback function when an asynchrounous operation has finished, and allows chaining of callbacks. Deferred basically has 2 callback chains, a done-chain and a fail-chain. If a asynchronous operation is successful the deferred should be resolved and the done callback chain of the deferred is called. If a asynchronous operation fails the deferred should be rejected and the fail callback chain of the deferred is called.

new Deferred()

Constructs a new Deferred

Source:
Returns:

A Deferred

Example
system.spawn("command").then(
     function() {
         // called when execution was  successful 
     },
     function(errorcode) {
         // called when execution wasn't successful 
     }
);

function foo() {
    var d = new Deferred();
    timerStart(2000, function() {
        d.reject("rejected");
    });
    return d;
}
function onResponse(response) {
    io.print(response);
}

// Will print "rejected" after 2 and 4 seconds
foo().fail(onResponse).fail(onResponse);

Members

<readonly> isFulfilled :Boolean

Wether this Deferred was resolved or rejected.

Source:

Methods

<static> when(value, ondone, onFail) → {Any value}

Static method that can be used for synchronous and asynchronous operations. If the first parameter is a Deferred ondone is called when the Deferred is resolved and onfail is called if the Deferred is rejected, otherwise ondone is called and value is the first parameter of the callback.

Parameters:
Name Type Description
value Value | Deferred

A Deferred or an arbitrary value

ondone Deferred~resolveCallback

Callback function for the done chain

onFail Deferred~rejectCallback

Callback function for the fail chain

Source:
Returns:

The value

Type
Any value
Example
 
function sync() {
    return  "sync";
}
function async() {
    var stdout;
    var d = new Deferred();
    timer.start(1000, function() {
        d.resolve("async");
        return false;
    });
    return d;
}
Deferred.when(sync(), function(response) {
    // sync
    io.print(response);
});
Deferred.when(async(), function(response) {
    // async
    io.print(response);
});

always(callback) → {Deferred}

Registers a function for the done- and fail-chain

Parameters:
Name Type Description
callback Deferred~resolveCallback | Deferred~rejectCallback

A callback function that will be called when the Deferred is resolved or rejected. If the function returns a deferred the original deferred will be replaced with the new deferred.

Source:
Returns:

A new Deferred that can be used to chain callbacks

Type
Deferred

done(callback) → {Deferred}

Registers a function for the done-chain

Parameters:
Name Type Description
callback Deferred~resolveCallback

A callback function that will be called when the Deferred is resolved. If the function returns a deferred the original deferred will be replaced with the new deferred.

Source:
Returns:

A new Deferred that can be used to chain callbacks

Type
Deferred

fail(callback) → {Deferred}

Registers a function for the fail-chain

Parameters:
Name Type Description
callback Deferred~rejectCallback

A callback function that will be called when the Deferred is rejected. If the function returns a deferred the original deferred will be replaced with the new deferred.

Source:
Returns:

A new Deferred that can be used to chain callbacks

Type
Deferred

reject(arguments)

Rejects a deferred, the fail-chain is called when a deferred is resolved

Parameters:
Name Type Description
arguments Object

Arguments passed to the fail callbacks

Source:

resolve(arguments)

Resolves a deferred, the done-chain is called when a deferred is resolved

Parameters:
Name Type Description
arguments Object

Arguments passed to the done callbacks

Source:

then(ondone, onfail) → {Deferred}

Registers functions for the done and fail chain

Parameters:
Name Type Description
ondone Deferred~resolveCallback

A callback function that will be called when the deferred is resolved. If the function returns a deferred the original deferred will be replaced with the new deferred.

onfail Deferred~rejectCallback

A callback function that will be called when the deferred is rejected. If the function returns a deferred the original deferred will be replaced with the new deferred.

Source:
Returns:

A new deferred that can be used to chain callbacks.

Type
Deferred

Type Definitions

rejectCallback(arguments)

Called when a Deferred is rejected

Parameters:
Name Type Description
arguments Object

Variable number of arguments passed to Deferred.reject

Source:

resolveCallback(arguments)

Called when a Deferred is resolved

Parameters:
Name Type Description
arguments Object

Variable number of arguments passed to Deferred.resolve

Source: