Does deferred / promise promote breaking the Law of Demeter?
deferred, javascript, law-of-demeter, promise
Solution
I think you're misinterpreting the meaning of Demeter's Law and its applicability to both languages like JavaScript and frameworks like promises.
Promises are not a "unit" in the sense that Demeter's Law envisions, which correspond to something like a "class" such as Account or Customer in a banking application. They are a higher-level, meta-construct for asynchronous control flow. It's hard to see how a such a meta-construct could even exist, or be useful, without being able to "talk to" arbitrary outside entities (non-friends) which it is designed to control.
Demeter's Law seems highly focused on classical OO systems where everything is a class or a method. As stated, it would appear to not allow any invocation whatsoever of passed-in functions, and thus most if not all of functional programming.
Another way of looking at this is that to the extent you view promises as violating this law, then callbacks certainly do too. After all, the two are basically isomorphic--the difference is essentially syntactic. So if you are fixated on not violating Demeter's law, you are also not going to be able to use callbacks--so how are you going to write the most basic asynchronous program?
Problem
I was in the shower and thought about something. The deferred / promise pattern is to decrease callback hell, by allowing the developer to chain call functions, as mentioned here: ``` Parse.User.logIn("user", "pass").then(function(user) { return query.find(); }).then(function(results) { return results[0].save({ key: value }); }).then(function(result) { // the object was saved. }); ``` From the top of my head - correct me if I am wrong - but it seems like using deferred / promises is an easy way to break the Law of Demeter? The Law of Demeter states: A method of an object may only call methods of: - The object itself. - An argument of the method. - Any object created within the method. - Any direct properties/fields of the object. Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. Or: Each unit should only talk to its friends; Don’t talk to strangers. Any comments concerning this? Update December 1 2013: A summarized version of my question. The Promise framework is designed to simplify asynchronous coding and avoid "callback hell". One of the most beneficial features of Promise is that you can chain-call events by using `.then()`, as seen in my example above. Given all code/functions are now using Promise (like Benjamin Gruenbaum (author below) is currently doing), won't it open it up to make chain-calling functions really easy by going `.then().then().then()` etc. Writing code that chain-call functions after each other (`.then().then().then()`) has to be a text-book example of how to break the Law of Demeter. Hence my question; Does the Promise framework promote / open up / make it easier to abuse / break the Law of Demeter?