Skip to content

Commit d571e85

Browse files
committed
Callback Hell leads to Inversion of Control
1 parent a859d64 commit d571e85

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,34 @@ getData(function (a) {
6767
Here are two solutions to this problem:
6868
* Name your functions and declare them and pass just the name of the function as the callback, instead of defining an anonymous function in the parameter of the main function. It makes code easier to read and also helps to get better stack traces when exeptions happen.
6969
* Modularity: Separate your code into modules, so you can export a section of code that does a particular job. Then you can import that module into your larger application.
70+
71+
## Callback Hell leads to **Inversion of Control**
72+
73+
```
74+
// A
75+
ajax( "..", function(..){
76+
// C
77+
} );
78+
// B
79+
```
80+
81+
// A and // B happen now, under the direct control of the main JS program. But // C gets deferred to happen later, and under the control of another party -- in this case, the ajax(..) function.
82+
In a basic sense, that sort of hand-off of control doesn't regularly cause lots of problems for programs.
83+
84+
A real world example would be
85+
86+
```
87+
trackCheckoutAjax(purchaseInfo, function() {
88+
chargeCreditCard(purchaseInfo);
89+
showThankYouPage();
90+
}
91+
```
92+
93+
But don't be fooled by its infrequency that this control switch isn't a big deal. In fact, it's one of the worst (and yet most subtle) problems about callback-driven design.
94+
It revolves around the idea that sometimes ajax(..) (i.e., the "party" you hand your callback continuation to) is not a function that you wrote, or that you directly control. Many times it's a utility provided by some third party.
95+
We call this "inversion of control," when you take part of your program and give over control of its execution to another third party. There's an unspoken "contract" that exists between your code and the third-party utility -- a set of things you expect to be maintained.
96+
In the case of `trackCheckoutAjax`, the 3rd party trackCheckoutAjax could potentially call the passed callback multiple times -- since we passed over the callback and really have no control over it being called.
97+
7098

7199
### Callback functions - Benefits
72100
* Do not repeat code (DRY—Do Not Repeat Yourself)
@@ -88,6 +116,7 @@ Here are two solutions to this problem:
88116
* [Callback Hell](http://callbackhell.com/)
89117
* [Avoiding Callback hell in Node.js](http://stackabuse.com/avoiding-callback-hell-in-node-js/)
90118
* [Syncing Async by Kyle Simpson](https://www.youtube.com/watch?v=-wYw0bZZ38Y)
119+
* [Callbacks - YDKJS](https://github.com/getify/You-Dont-Know-JS/blob/master/async%20&%20performance/ch2.md)
91120

92121
## [Promises](2-promises.js)
93122
Promises are usually vaguely defined as “a proxy for a value that will eventually become available”. They can be used for both synchronous and asynchronous code flows, although they make asynchronous flows easier to reason about.

0 commit comments

Comments
 (0)