5.12 Pre-Triggered Deferred Factory Methods

In addition to the conventional newDeferred factory method for deferred event objects, the reactor provides two methods for generating pre-triggered deferred event objects. These are useful in situations where an asynchronous API call can return an immediately available value as the callback parameter or an immediately generated exception as the errback parameter. The callDeferred convenience method takes the data value which is to be passed as the callback parameter and creates a new deferred event object where the onCallback method has already been invoked using that data value. Similarly, the failDeferred convenience method takes the exception object which is to be passed as the errback parameter and creates a new deferred event object where the onErrback method has already been invoked.


\begin{listing}
% latex2html id marker 1405\begin{small}
\begin{verbatim}......
...m}
\end{small}\caption{Using Pre-Triggered Deferred Event Objects}
\end{listing}

An example of using pre-triggered deferred event objects is shown in Listing 5.18. In this case, the desired return value may already have been accessed and locally cached. If present, the cached value can be returned immediately using the callDeferred method. Alternatively, a cached error condition can be returned immediately using the failDeferred method. If no cached value is present, a conventional asynchronous call can be made, which as well as returning the data value to the caller can also be configured to update the cached values when the callback chain is executed.