deferred.js
No description.

File Location

arty/closure/goog/mochikit/async/deferred.js

Classes

goog.async.Deferred
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.} */ 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.
goog.async.Deferred.AlreadyCalledError
An error sub class that is used when a Deferred has already been called.
goog.async.Deferred.CanceledError
An error sub class that is used when a Deferred is canceled.
goog.async.Deferred.Error_
Wrapper around errors that are scheduled to be thrown by failing deferreds after a timeout.

Public Protected Private

Global Functions

goog.async.Deferred.assertNoErrors()
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 »
goog.async.Deferred.canceled() !goog.async.Deferred
Creates a Deferred that has already been canceled.
Returns: !goog.async.Deferred  The new Deferred.
code »
goog.async.Deferred.fail(res) !goog.async.Deferred
Creates a Deferred that has an initial error result.
Arguments:
res : *
The error result.
Returns: !goog.async.Deferred  The new Deferred.
code »
goog.async.Deferred.fromPromise(promise) !goog.async.Deferred.<T>
Creates a Deferred that fires when the given promise resolves. Use only during migration to Promises.
Arguments:
promise : !goog.Promise.<T>
No description.
Returns: !goog.async.Deferred.<T>  The new Deferred.
code »
goog.async.Deferred.scheduleError_(error) number
Schedules an error to be thrown after a delay.
Arguments:
error : *
Error from a failing deferred.
Returns: number  Id of the error.
code »
goog.async.Deferred.succeed(opt_result) !goog.async.Deferred
Creates a Deferred that has an initial result.
Arguments:
opt_result : *=
The result.
Returns: !goog.async.Deferred  The new Deferred.
code »
goog.async.Deferred.unscheduleError_(id)
Unschedules an error from being thrown.
Arguments:
id : number
Id of the deferred error to unschedule.
code »
goog.async.Deferred.when(valuecallbackopt_scope) !goog.async.Deferred
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:
value : *
Deferred or normal value to pass to the callback.
callback : !?function(this:T, ):?
The callback to execute.
opt_scope : T=
An optional scope to call the callback in.
Returns: !goog.async.Deferred  A new Deferred that will call the input callback with the input value.
code »

Directory async

File Reference