title: How do I rewrite a series of conditional statements with Q promises in node.js?
body: <pre><code>exports.create = function(req, res) {
    var company_id = req.company_id;
    var client = new Client(req.body);
  
    Company.findOne({_id: company_id}, function(err, company) {
      if(err) {
        response = {
          status: 'error',
          error: err
        }
  
        return res.json(response);
      } else if(!company) {
        response = {
          status: 'error',
          error: 'Invalid company_id'
        }
  
        return res.json(response);
      } else {
        client.save(function(err) {
          if(err) {
            response = {
              status: 'error',
              error: err
            }
          } else {
            response = {
              status: 'ok',
              client: client
            }
          }
  
          return res.json(response);
        });
      }
    });
  }
  </code></pre>
  
  <p>That's my code (using <code>Express</code>, if it matters). I'm trying to learn more about promises, specifically with <code>Q</code>. I feel that this is the perfect kind of logic that can be implemented with promises to avoid this hairy conditional nest. But I'm not sure how to start?</p>