Deferred

The Deferred type provides a concrete implementation of the Promises/A+ specification that represents an operation that completes at some point in the future, optionally producing a result. A Deferred is engineered to be instantiated and later fulfilled or rejected, exposing a Promise whose handlers / callbacks are invoked appropriately.

Constructor

Deferred()

Constructs a new instance of a Deferred that may be rejected or fulfilled.

Methods

fulfill(result)

Fulfills the instance, accepting an optional result. When invoked, all onFulfilled functions attached via the then method will be invoked at the earliest opportunity after this method completes. Each onFulfilled method will be provided the result value specified.

Parameters

result (optional)
The optional value produced or returned by the instance.

Return Value

This method returns no value.

getState()

Obtains the current state of the Deferred instance.

Return Value

The current state of the instance, where the possible values are defined by the Deferred.States enumeration.

promise()

Obtains the Promise representation of this instance.

Return Value

The Promise representation of this instance. This exposes only the then method of the Deferred, providing a safe data structure that can be passed to other methods without allowing modification of the underlying Deferred.

reject(reason)

Rejects the instance, accepting an optional reason. When invoked, all onRejected functions attached via the then method will be invoked at the earliest opportunity after this method completes. Each onRejected method will be provided the reason value specified.

Parameters

reason (optional)
The optional reason that the instance was rejected.

Return Value

This method returns no value.

then(onFulfilled, onRejected)

Registers methods to be invoked when the current instance is either fulfilled or rejected, returning a new Promise that represents the return value (if any) of the invoked method.

Parameters

onFulfilled (optional)
A method that is called after the promise is fulfilled. The fulfillment value of the promise (if present) is provided to this method as the first parameter when it is invoked. This method is invoked either one time (if the promise is fulfilled) or not at all (if the promise is rejected). If this parameter is not a function, it is ignored.
onRejected (optional)
A method that is called after the promise is rejected. The rejection reason of the promise (if present) is provided to this method as the first parameter when it is invoked. This method is invoked either one time (if the promise is rejected) or not at all (if the promise is fulfilled). If this parameter is not a function, it is ignored.

Return Value

A new Promise that is fulfilled or rejected after onFulfilled or onRejected has been invoked as a result of the completion of the original Promise (i.e. a continuation). If both onFulfilled and onRejected are ignored (i.e. not functions), this method returns the original Promise instance, not a new one.

The fulfillment value or rejection reason (if any) of the new promise is the return value produced by either onFulfilled or onRejected, whichever is invoked. If the return value of onFulfilled or onRejected is a Promise, the Promise returned by the then method assumes the state of this inner Promise. See the Promises/A+ specification or the examples for more information.

Enumerations

Deferred.States

A numeric value indicating the state of a Deferred. These states are reflected by the Promises/A+ specification. Possible values are:

Pending
Awating completion (neither rejected nor fulfilled).
Fulfilled
Completed successfully.
Rejected
Completed erroneously.

Last edited Sep 15, 2013 at 6:56 AM by CommanderQ, version 5