This is not a bad question. However, there are several ways to completely destroy your control. Note. I did not say that it was beautiful.
Suppose you have objects A, B, C, and D, A.Amethod
nothing and calls the getBData
method from B, B calls the getCData
method for C, and problem C causes D something like this
var data = D.getRawData(); ... something to be done with data ... ... something else to be done with data...
and now it should be written as
D.getData(function(data){ ... something to be done with data ... ... something else to be done with data... });
well, you can always add a callback parameter for each of your methods so that the code uses the code that looks like this:
var A = {
Now you will have:
var A = { ... Amethod: function(x,y,z){ this.B.getBData((1,2,3,function(somethingForA){ astatement1; astatement2; }); ... } ... }
This is a fairly simple refactoring using anonymous functions to implement all the functions that support the control flow. I do not know how much this would be a literal transformation; you may have to do some tuning for the scope variable. And obviously this is not so pretty.
Are there any other ways? Of course. As you can see, the above is useless, and we hope that we will not write dirty code. How less randomly depends on the context.
You do not need to pass a callback parameter, any necessary callback can be passed to objects in advance or can be passed as one of the elements in the parameter. The callback may not necessarily have to be called directly, but indirectly called using one of the available event handling methods (which you will need to see which libraries you can use to do this), and the data can be transferred when the event fires. Or maybe there is a global "DataGetter" that can register a callback whenever the data is "received" to completely avoid the intermediaries B and C.
Finally, there is the consideration that if you are deep in the bones, you will find that you need something that can be obtained asynchronously, and that the data is transmitted in a chain, you could do something a little back in terms of which objects should control the flow program logic (I'm honestly a dead end on how to describe why I seem to think this scenario is problematic, though.). I am inclined to think that since instances of A must contain instances of B, B, contain instances of C, etc., Instances creating auxiliary instances as part of its composition should have some degree of control as to how the sub-instances themselves are filled, instead of letting the sub-instances completely solve .... if that makes sense :-(
At this moment, I feel like I'm a little confused ... here, to hope that someone will explain the problems better than me!