Home

# Class goog.async.Deferred

Represents the results of an asynchronous operation. A Deferred object starts with no result, and then gets a result at some point in the future.

Instance Method Summary
| `addBoth(!Function f, ?Object= opt_scope) ⇒ !goog.async.Deferred` Registers a function as both callback and errback.
| `addCallback(!Function cb, ?Object= opt_scope) ⇒ !goog.async.Deferred` Register a callback function, to be called when a successful result is available.
| `addCallbacks(?Function cb, ?Function eb, ?Object= opt_scope) ⇒ !goog.async.Deferred` Registers a callback function and errback function.
| `addErrback(!Function eb, ?Object= opt_scope) ⇒ !goog.async.Deferred` Register a callback function, to be called if this operation fails.
| `assertNotDeferred_(<Any Type> obj)` Asserts that an object is not a Deferred.
| `awaitDeferred(!goog.async.Deferred otherDeferred) ⇒ !goog.async.Deferred` Makes this Deferred wait for otherDeferred to be called, and its preceding callbacks to be executed, before continuing with the callback sequence. This is equivalent to adding a callback that returns otherDeferred, but doesn't prevent additional callbacks from being added to otherDeferred.

`branch(boolean= opt_propagateCancel) ⇒ !goog.async.Deferred`

Create a branch off this Deferred's callback chain, and return it as a new Deferred. This means that the return value will have the value at the current point in the callback chain, regardless of any further callbacks added to this Deferred. Additional callbacks added to the original Deferred will not affect the value of any branches. All branches at the same stage in the callback chain will receive the same starting value.

| `branchCancel_()` Handle a single branch being cancelled. Once all branches are cancelled, this Deferred will be cancelled as well.
| `callback(<Any Type> result)` Record a successful result for this operation, and send the result to all registered callback functions.

`cancel(boolean= opt_deepCancel)`

Cancels a deferred that has not yet received a value. If this Deferred is paused waiting for a chained Deferred to fire, the chained Deferred will also be cancelled. If this Deferred was created by calling branch() on a parent Deferred with opt_propagateCancel set to true, the parent may also be cancelled. If opt_deepCancel is set, cancel() will be called on the parent (as well as any other ancestors if the parent is also a branch). If one or more branches were created with opt_propagateCancel set to true, the parent will be cancelled if cancel() is called on all of those branches.

| `chainDeferred(!goog.async.Deferred otherDeferred) ⇒ !goog.async.Deferred` Adds another deferred to the end of this deferred's processing chain. Use this when you want otherDeferred to be called at the end of thisDeferred's previous callbacks.
| `check_()` Verifies that the deferred has not yet been fired.
| `continue_(boolean isSuccess, <Any Type> res)` Called when a dependent deferred fires.
| `errback(<Any Type> result)` Record that this operation failed with an error, and send the error to all registered errback functions.
| `fire_()` Exhausts the callback sequence once a result is available.
| `hasErrback_() ⇒ boolean`
| `hasFired() ⇒ boolean`
| `isError(<Any Type> res) ⇒ boolean`
| `pause_()` Pauses the deferred.
| `resback_(boolean isSuccess, <Any Type> res)` Called when either a success or a failure happens.
| `unpause_()` Resumes a paused deferred.

Static Method Summary
| `cancelled() ⇒ !goog.async.Deferred` Creates a deferred that has already been cancelled.
| `fail(<Any Type> res) ⇒ !goog.async.Deferred` Creates a deferred that always fails.
| `succeed(<Any Type> res) ⇒ !goog.async.Deferred` Creates a deferred that always succeeds.

`when(<Any Type> value, !Function callback, ?Object= opt_scope) ⇒ !goog.async.Deferred`

Applies a callback to both deferred and non-deferred values, providing a mechanism to normalize synchronous and asynchronous behavior. If the value is non-deferred, the callback will be executed immediately and an already committed deferred returned. If the object is a deferred, it is branched (so the callback doesn't affect the previous chain) and the callback is added to the new deferred. The branched deferred is then returned. 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);