Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. /*
  2. we can uninvert the inversion of control (i.e. take back control) by, instead of passing callbacks to our functions,
  3. we have our functions return an event capability we will refer to as evt that receives callbacks
  4. */
  5.  
  6. function foo(x){
  7. //start doing something could take a while
  8. //make a `listener` event notification
  9. //capability to retunr
  10.  
  11. return listener;
  12. }
  13.  
  14. var evt = foo(42);
  15.  
  16. //let `bar(..)` listen to `foo(..)`'s completion
  17. bar(evt);
  18.  
  19. //also let `baz(..)` listen to `foo(..)`'s completion
  20. baz(evt);
  21.  
  22. /*
  23. foo(..) expressly creates an event subscription capability to return back,
  24. and the calling code receives and registers the two event handlers against it
  25. One important benefit is that multiple seperate parts of the code can be given
  26. the event listening capability, and they can all independently be notified of when
  27. foo(..) completes to perform subsequent steps after its completion
  28.  
  29. Uninversion of control enables a nice seperation of concerns -
  30. bar(..) and baz(..) don't need to be involved in how foo(..)
  31. is called. Similarly foo(..) doesn't need to know or care that bar(..)
  32. and baz(..) exist or are waiting to be notified when foo(..) completes
  33.  
  34. essentially, the evt object is a neutral third-party mediator
  35. between the separate concerns
  36.  
  37. The evt object is an analogy for a Promise
  38.  
  39. In a promise-based approach, the previous snipped would have foo(..)
  40. creating and returning a Promise instance and that promise would then be
  41. passed to bar(..) and baz(..)
  42. */
  43.  
  44. function foo(x){
  45. //start doing something that could take a while
  46.  
  47. //construct and return a promise
  48.  
  49. //called the revealing contructor
  50. return new Promsie(function(resolve, reject){
  51. //eventually, call `resolve(..)` or `reject(..)`,
  52. //which are the resolution callbacks for the promise
  53. });
  54. }
  55.  
  56. var p = foo(42);
  57.  
  58. // with this approach, bar() si callred regardless of whether foo(..)
  59. //succeeds or fails, and it handles its own fallback logic if it's notified
  60. //that foo(..) failed
  61. bar(p);
  62.  
  63. baz(p);
  64.  
  65. // we pass the promise to bar(..)
  66. function bar(fooPromise){
  67. //listen for `foo(..)` to complete
  68. fooPromise.then(
  69. function(){
  70. // `foo(..)` has now finished, so
  71. // do `bar(..)`'s task
  72. },
  73. function(){
  74. //oops, something went wrong in `foo(..)`
  75. }
  76. );
  77. }
  78.  
  79. // ditto for `baz(..)`
  80.  
  81. //We can do this another way:
  82.  
  83. function bar(){
  84. //`foo(..)` has definitely finished so do `bar(..)` next
  85. }
  86.  
  87. function oopsBar(){
  88. //oops, something went wrong in `foo(..)`, so `bar(..)` didnt run
  89. }
  90.  
  91. //ditto for `baz()` and `oopsBaz()`
  92.  
  93. var p = foo(42);
  94.  
  95. //bar is only called if p succeeds, else oopsBar is called
  96. p.then(bar, oopsBar);
  97.  
  98. p.then(baz, oopsBaz);
  99.  
  100. /*
  101. Above, instead of passing the p promise to bar(..) and baz(..),
  102. we use the promise to control when bar(..) and baz(..) will get executed, if ever
  103. The primary difference is in the error handling
  104.  
  105. With this approach, bar(..) only gets called if foo(..) succeeds,
  106. and otherwise, oopsBar(..) gets called.
  107. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement