title: How to stop execution of a node.js script?
body: <p>Say I've got this script:</p>
  
  <pre><code>var thisIsTrue = false;
  
  exports.test = function(request,response){
  
      if(thisIsTrue){
          response.send('All is good!');
      }else{
          response.send('ERROR! ERROR!');
          // Stop script execution here.
      }
  
      console.log('I do not want this to happen if there is an error.');
  
  }
  </code></pre>
  
  <p>And as you can see, I'd like to stop the script from executing any downstream functions if there's an error.</p>
  
  <p>I've managed to achieve this by adding <code>return;</code> after the error response is sent:</p>
  
  <pre><code>var thisIsTrue = false;
  
  exports.test = function(request,response){
  
      if(thisIsTrue){
          response.send('All is good!');
      }else{
          response.send('ERROR! ERROR!');
          return;
      }
  
      console.log('I do not want this to happen if there is an error.');
  
  }
  </code></pre>
  
  <p>But is that the 'correct' way to do things?</p>
  
  <p><strong>Alternatives</strong></p>
  
  <p>I've also seen examples that use <code>process.exit();</code> and <code>process.exit(1);</code>, but that gives me a <code>502 Bad Gateway</code> error (I assume because it kills node?).</p>
  
  <p>And <code>callback();</code>, which just gave me an 'undefined' error.</p>
  
  <p>What is the 'correct' way to stop a node.js script at any given point and prevent any downstream functions from executing?</p>