5.14 Fluent-Style Deferred Programming

The API for deferred event objects as shown in Listing 5.2 also provides support for fluent-style programming. Since most of the API calls return an appropriately typed reference to the deferred event object itself, it is possible to chain together multiple method calls within a single statement. An example of this approach is shown in Listing 5.19.


\begin{listing}
% latex2html id marker 1422\begin{small}
\begin{verbatim}......
...nd{small}\caption{Building a Fluent-Style Deferred Callback Chain}
\end{listing}

In order to make code written in this manner more elegant, a reduced-form version of the addDeferrable method is provided which omits the terminal parameter. All deferrable callbacks which are added via this method will then be appended to the callback chain without terminating it. The additional terminate method is provided for explicitly terminating callback chains which have been assembled in this manner

Since the setTimeout, cancelTimeout and makeRestricted API calls return an appropriately typed reference to the deferred event object, these may also be used in a fluent-style manner. As well as building the callback chain, the supplied example shows the additional assignment of a 500 millisecond timeout.