Deferreds
JavaScript doesn't currently support the concept of threads, but
it does offer the ability to perform asynchronous requests via the
XMLHttpRequest object and through
delays with the setTimeout
function. However, it doesn't take too many asynchronous calls running
around before matters get awfully confusing. Base provides a class
called Deferred to help manage the
complexity often associated with the tedious implementation details of
asynchronous events. Like other abstractions, Deferred s allow you to hide away tricky
logic and/or boilerplate into a nice, consistent interface.
If the value of a Deferred was described in one sentence, however, it would probably be that it enables you to treat all network I/O uniformly regardless of whether it is synchronous or asynchronous. Even if a Deferred is in flight, has failed, or finished successfully, the process for chaining callbacks and errbacks is the exact same. As you can imagine, this behavior significantly simplifies bookkeeping.
Tip
Dojo's implementation of a Deferred is minimally adapted from
MochiKit's implementation, which in turn is inspired from Twisted's
implementation of the same. Some good background on MochiKit's
implementation is available at http://www.mochikit.com/doc/html/MochiKit/Async.html#fn-deferred.
Twisted's implementation of Deferred s is available at http://twistedmatrix.com/projects/core/documentation/howto/defer.html.
Some key features of Deferred s are that they allow you to chain together multiple ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access