title: How to reject (and properly use) Promises?
body: <p><em>Short story</em>:</p>
  
  <ul>
  <li><p>Talking about <strong>Promises/A+</strong>, what is the proper way to reject a promise - throwing an error? But if I miss the <code>catch</code> - my whole app will blow!</p></li>
  <li><p>How to use <code>promisify</code> and what are the benefits of it (maybe you'll need to read the longer version)?</p></li>
  <li><p>Is <code>.then(success, fail)</code> really an <a href="https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns">anti-pattern</a> and should I always use <code>.then(success).catch(error)</code>?</p></li>
  </ul>
  
  <p><em>Longer version</em>, described as real life problem (would love someone to read):</p>
  
  <p>I have a library that uses <a href="https://github.com/petkaantonov/bluebird">Bluebird</a> (A+ Promise implementation library), to fetch data from database (talking about <a href="http://sequelize.readthedocs.org/en/latest/">Sequelize</a>). Every query returns a result, but sometimes it's empty (tried to select something, but there wasn't any). The promise goes into the <code>result</code> function, because there is no reason for an error (not having results is not an error). Example:</p>
  
  <pre><code>Entity.find(1).then(function(result) {
    // always ending here, despite result is {}
  }).catch(function(error) {
    // never ends here
  });
  </code></pre>
  
  <p>I want to wrap this and check if the result is empty (can't check this everywhere in my code). I did this:</p>
  
  <pre><code>function findFirst() {
    return Entity.find(1).then(function(result) {
      if (result) { // add proper checks if needed
        return result; // will invoke the success function
      } else {
        // I WANT TO INVOKE THE ERROR, HOW?! :)
      }
    }).catch(function(error) {
      // never ends here
    });
  }
  
  findFirst().then(function(result) {
    // I HAVE a result
  }).catch(function(error) {
    // ERROR function - there's either sql error OR there is no result!
  });
  </code></pre>
  
  <p>If you are still with me - I hope you will understand what's up. I want somehow to fire up the error function (where "ERROR function" is). The thing is - I don't know how. I've tried these things:</p>
  
  <pre><code>this.reject('reason'); // doesn't work, this is not a promise, Sequelize related
  return new Error('reason'); // calls success function, with error argument
  return false; // calls success function with false argument
  throw new Error('reason'); // works, but if .catch is missing =&gt; BLOW!
  </code></pre>
  
  <p>As you can see by my comments (and per spec), throwing an error works well. But, there's a big <strong>but</strong> - if I miss the <code>.catch</code> statement, my whole app blows up.</p>
  
  <p>Why I don't want this? Let's say I want to increment a counter in my database. I don't care about the result - I just make HTTP request.. So I can call <code>incrementInDB()</code>, which has the ability to return results (even for test reasons), so there is <code>throw new Error</code> if it failed. But since I don't care for response, <strong>sometimes I won't add .catch statement</strong>, right? But now - if I don't (on purpose or by fault) - I end up with your node app down.</p>
  
  <p><strong>I don't find this very nice</strong>. Is there any <strong>better</strong> way to work it out, or I just have to deal with it?</p>
  
  <p>A friend of mine helped me out and I used a new promise to fix things up, like this:</p>
  
  <pre><code>function findFirst() {
    var deferred = new Promise.pending(); // doesnt' matter if it's Bluebird or Q, just defer
    Entity.find(1).then(function(result) {
      if (result) { // add proper checks if needed
        deferred.resolve(result);
      } else {
        deferred.reject('no result');
      }
    }).catch(function(error) {
      deferred.reject('mysql error');
    );
  
    return deferred.promise; // return a promise, no matter of framework
  }
  </code></pre>
  
  <p>Works like a charm! <strong>But</strong> I got into this: <a href="https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns">Promise Anti Patterns</a> - wiki article written by Petka Antonov, creator of Bluebird (A+ implementation). It's explicitly said that this is <strong>wrong</strong>.</p>
  
  <p>So my second question is - is it so? If yes - why? And what's the best way?</p>
  
  <p>Thanks a lot for reading this, I hope someone will spent time to answer it for me :) I should add that I didn't want to depend too much on frameworks, so <code>Sequelize</code> and <code>Bluebird</code> are just things that I ended up working with. My problem is with Promises as a global, not with this particular frameworks.</p>