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.} */
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.
![]()
Asserts that there are no pending deferred errors. If there are any
scheduled errors, one will be thrown immediately to make this function fail.
|
code » | ||||
Creates a Deferred that has already been canceled.
Returns: !goog.async.Deferred
The new Deferred.
|
code » | ||||
Creates a Deferred that has an initial error result.
Arguments:
Returns: !goog.async.Deferred
The new Deferred.
|
code » | ||||
Creates a Deferred that fires when the given promise resolves.
Use only during migration to Promises.
Arguments:
|
code » | ||||
Schedules an error to be thrown after a delay.
Arguments:
Returns: number
Id of the error.
|
code » | ||||
Creates a Deferred that has an initial result.
Arguments:
Returns: !goog.async.Deferred
The new Deferred.
|
code » | ||||
![]()
Unschedules an error from being thrown.
Arguments:
|
code » | ||||
Normalizes values that may or may not be Deferreds.
If the input value is a Deferred, the Deferred is branched (so the original
execution sequence is not modified) and the input callback added to the new
branch. The branch is returned to the caller.
If the input value is not a Deferred, the callback will be executed
immediately and an already firing Deferred will be returned to the caller.
In the following (contrived) example, if
isImmediate is true
then 3 is alerted immediately, otherwise 6 is alerted after a 2-second delay.
var value; if (isImmediate) { value = 3; } else { value = new goog.async.Deferred(); setTimeout(function() { value.callback(6); }, 2000); } var d = goog.async.Deferred.when(value, alert);
Arguments:
Returns: !goog.async.Deferred
A new Deferred that will call the input
callback with the input value.
|
code » |