search

goog.async.Deferred<VALUE>

Provided By
All Implemented Interfaces
All Known Direct Subclasses

A Deferred represents the result of an asynchronous operation. A Deferred instance has no result when it is created, and is "fired" (given an initial result) by calling callback or errback.

Once fired, the result is passed through a sequence of callback functions registered with addCallback or addErrback. The functions may mutate the result before it is passed to the next function in the sequence.

Callbacks and errbacks may be added at any time, including after the Deferred has been "fired". If there are no pending actions in the execution sequence of a fired Deferred, any new callback functions will be called with the last computed result. Adding a callback function is the only way to access the result of the Deferred.

If a Deferred operation is canceled, an optional user-provided cancellation function is invoked which may perform any special cleanup, followed by firing the Deferred's errback sequence with a CanceledError. If the Deferred has already fired, cancellation is ignored.

Deferreds may be templated to a specific type they produce using generics with syntax such as:

/** @type {goog.async.Deferred<string>} *\
var d = new goog.async.Deferred();
// Compiler can infer that foo is a string.
d.addCallback(function(foo) {...});
d.callback('string');  // Checked to be passed a string

Since deferreds are often used to produce different values across a chain, the type information is not propagated across chains, but rather only associated with specifically cast objects.

new Deferred<VALUE>( opt_onCancelFunction, opt_defaultScope )

Parameters
opt_onCancelFunction(Function|null)=

A function that will be called if the Deferred is canceled. If provided, this function runs before the Deferred is fired with a CanceledError.

opt_defaultScope(Object|null)=

The default object context to call callbacks and errbacks in.

Instance Methods

Static Functions

Compiler Constants

Classes