All work is currently in the develop branch
A detailed README is also availble in the develop branch.
The repository provides a AS3 library of futures as defined by CommonJS.
A promise (aka Future) is an object thats acts as a proxy for a result that my not be initially known, usually because the computation of its value has not yet completed. Developers defer processing by returning a promise to respond later. A promiseis essentially a read-only version of the deferredresponse.
-
new Deferred()
- can add callbacks
- can be resolved or rejected
- can **promise()**to let you know what happened
-
Promise (accessed via
new Deferred().promise
)- can add callbacks
- Can't resolve, so you know it's legit when it does
- Can check resolve status
- Can cancel (only in AS3 version)
Popularized in the jQuery javascript library, Deferred(s) are now available for AS3 developers. This library emulates the jQuery v1.7 feature set of futures: Deferred, Promise, and Callbacks. This library also supports two syntactical approaches familiar to either jQuery users or Actionscript/Flex developers.
Flex developers often use components asynchronously:
- setTimeOut()
- setInterval()
- callLater()
- Timer
- Loader
- URLLoader
- HTTPService
- RemoteObject
- WebService
- NetStream
- Socket
- NetConnection
- SharedObject
The above components, unfortunately, do not have a consistent mechanism in which developers may handle the asynchronous responses… until now! With the above components developers would use:
- addEventListener()
- Responders
- AsyncToken
- closures (aka callbacks)
Now developers can employ Deferred.as
to build promises of responses. The interface is always the same regardless of the component/mechanism used to fulfill the promise.
The biggest advantages are
- Consistent, intuitive API for attaching handlers to asynchronous processes
- Ability to intercept and transform results before handlers are called
- Ability to chain futures in sequence
- Ability to process futures in parallel (aka batch processing)
- Crockford on JavaScript - Act III: Function the Ultimate
- Deferreds into jQuery
- Deferreds - Putting Laziness to Work
- Creating Responsive Applications Using jQuery Deferred and Promises
- The Power Of Closures - Deferred Object Bindings In jQuery
- Fun with jQuery Deferred
- Understanding jQuery.Deferred and Promise
- A Graphical Explanation Of Javascript Closures In A jQuery Context
- From callbacks... to $.Deferred... to $.Callbacks
- Using Promises to bind Button clicks
- Demystifying jQuery 1.7′s $.Callbacks