title: exception handling, thrown errors, within promises
body: <p>I am running external code as a 3rd party extension to a node.js service. The API methods return promises. A resolved promise means the action was carried out successfully,  a failed promise means there was some problem carrying out the operation.</p>
  
  <p>Now here's where I'm having trouble.</p>
  
  <p>Since the 3rd party code is unknown, there could be bugs, syntax errors, type issues, any number of things that could cause node.js to throw an exception.</p>
  
  <p>However, since all the code is wrapped up in promises, these thrown exceptions are actually coming back as failed promises.</p>
  
  <p>I tried to put the function call within a try/catch block, but it's never triggered:</p>
  
  <pre><code>// worker process
  var mod = require('./3rdparty/module.js');
  try {
    mod.run().then(function (data) {
      sendToClient(true, data);
    }, function (err) {
      sendToClient(false, err);
    });
  } catch (e) {
    // unrecoverable error inside of module
    // ... send signal to restart this worker process ...
  });
  </code></pre>
  
  <p>In the above psuedo-code example, when an error is thrown it turns up in the failed promise function, and not in the catch.</p>
  
  <p>From what I read, this is a feature, not an issue, with promises. However I'm having trouble wrapping my head around why you'd always want to treat exceptions and expected rejections exactly the same. </p>
  
  <p>One case is about actual bugs in the code, possibly irrecoverable --  the other is just possible missing configuration information, or a parameter, or something recoverable.</p>
  
  <p>Thanks for any help!</p>