What are the differences between Deferreds, Promises and Futures? Is there a generally approved theory behind all these three?
|
|
In light of apparent dislike for how I've attempted to answer the OP's question. The literal answer is, a promise is something shared w/ other objects, while a deferred should be kept private. Primarily, a deferred (which generally extends Promise) can resolve itself, while a promise might not be able to do so. If you're interested in the minutiae, then examine Promises/A+. So far as I'm aware, the overarching purpose is to improve clarity and loosen coupling through a standardized interface. See suggested reading from @jfriend00:
Personally, I've found deferred especially useful when dealing with e.g. templates that're populated by asynchronous requests, loading scripts that have networks of dependencies, and providing user feedback to form data in a non-blocking manner. Indeed, compare the pure callback form of doing something after loading CodeMirror in JS mode asynchronously (apologies, I've not used jQuery in a while):
To the promises formulated version (again, apologies, I'm not up to date on jQuery):
Apologies for the semi-psuedo code, but I hope it makes the core idea somewhat clear. Basically, by returning a standarized promise, you can pass the promise around, thus allowing for more clear grouping. |
|||||||||||||||||
|
|
What really made it all click for me was this presentation by Domenic Denicola. In a github gist, he gave the description I like most, it's very concise:
In other word, promises are a way that lets us write asynchronous code that is almost as easy to write as if it was synchronous. Consider this example, with promises:
It works as if you were writing this synchronous code:
(If this still sounds complicated, watch that presentation!) Regarding Deferred, it's a way to Please note that, as far as I know, the Promise implementation in jQuery is broken (see that gist), at least as of jQuery 1.8.2. If you are going to use Promises (you should try them out with your own code!), use Kris Kowal's Q. The jQuery version is just some callback aggregator for writing cleaner jQuery code, but misses the point. Regarding Future, I have no idea, I haven't seen that in any API. Edit: Domenic Denicola's youtube talk on Promises from @Farm's comment below. A quote from Michael Jackson (yes, Michael Jackson) from the video:
This is an excellent description: a promise is like a variable from the future - a first-class reference to something that, at some point, will exist (or happen). |
|||||||||||||||||||||
|
|
These answers, including the selected answer, are good for introducing promises conceptually, but lacking in specifics of what exactly the differences are in the terminology that arises when using libraries implementing them (and there are important differences). Since it is still an evolving spec, the answer currently comes from attempting to survey both references (like wikipedia) and implementations (like jQuery):
References
Misc potentially confusing things
|
|||||
|
AFAIK, they are basically the same when spoken in context of JavaScript. The literature might have slight nuances but basically, they are the same.
If you are trying to understand what is the theory behind this pattern, I have my thoughts summarized here: Promises in JavaScript |
|||||||||||||||||
|
|
"-a promise represents a value that is not yet known -a deferred represents work that is not yet finished A promise is a placeholder for a result which is initially unknown while a deferred represents the computation that results in the value." http://blog.mediumequalsmessage.com/promise-deferred-objects-in-javascript-pt1-theory-and-semantics |
|||
|
|