Last active
          December 14, 2015 05:28 
        
      - 
      
 - 
        
Save valueof/5035108 to your computer and use it in GitHub Desktop.  
    [Code Review] Trying to wrap my head around promises. Here's my attempt at rewriting one somewhat nested function. I'm not sure how to stop propagation from within a promise callback so, for the rewritten version, I changed `this.controller.isActive` to reject the promise if `isActive` is `false`. I don't really know if this code even works, I w…
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ProfilerPanel.prototype = { | |
| stopProfiling: function () { | |
| this.controller.isActive(function (err, isActive) { | |
| if (err) { | |
| Cu.reportError("ProfilerController.isActive: " + err.message); | |
| return; | |
| } | |
| if (!isActive) { | |
| return; | |
| } | |
| this.controller.stop(function (err, data) { | |
| if (err) { | |
| Cu.reportError("ProfilerController.stop: " + err.message); | |
| return; | |
| } | |
| this.activeProfile.parse(data, function onParsed() { | |
| this.emit("parsed"); | |
| }.bind(this)); | |
| onStop(); | |
| this.emit("stopped"); | |
| }.bind(this)); | |
| }.bind(this)); | |
| } | |
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ProfilerPanel.prototype = { | |
| stopProfiling: function () { | |
| let {emit, controller, activeProfile} = this; | |
| let isActive = controller.isActive(); | |
| let stop = function () { | |
| return controller.stop().then(function emit("stopped")); | |
| }; | |
| let parse = function (data) { | |
| activeProfile.parse(data, function emit("parsed")); | |
| } | |
| isActive.then(stop).then(parse).then(onStop, function (err) { | |
| if (err) { | |
| Cu.reportError("ProfilerController.stopProfiling: " + err.message); | |
| } | |
| }); | |
| } | |
| }; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
@domenic
if (err)is to distinguish between the case whenisActivefails and the case when it simply returnsfalse. It's unnecessary if I move asyncisActiveinto thecontroller.stop.