title: Returning a custom promise
body: <p>I'm having some issues getting a custom promise to work with my little app I'm working on.</p>
  
  <p>Here is an abbreviated version of my <code>app.js</code></p>
  
  <pre><code>var Promise = require('promise');
  var Commands = require('./commands');
  
  var respond = function() {
      var messageToRespondWith = command[0].response;
      if(typeof(command[0].response) == "function"){
          console.log('is function');
          command[0].response().then(console.log('finished'));
      }
      var response = template(messageToRespondWith, {user: user, channel: channel, message: message});
      client.say(channel, response);
  };
  </code></pre>
  
  <p>And the commands look something like this</p>
  
  <pre><code>var config = require('./config');
  var Promise = require('promise');
  
  module.exports = [
    {
      trigger: "!random",
      response: function(){
        var unirest = require('unirest');
        var promise = new Promise(function(resolve, reject) {
        unirest.post("https://andruxnet-random-famous-quotes.p.mashape.com/cat=movies")
        .header("X-Mashape-Key", config.mashapeKey)
        .header("Content-Type", "application/x-www-form-urlencoded")
        .header("Accept", "application/json")
        .end(function (result) {
          console.log(result.status);
          // Handle errors
          if(result.status != 200){
            return promise.reject(result.body);
          }else{
            var json = JSON.parse(result.body);
            return promise.resolve(json.quote);
          }
        });
      });
    },
    permission: null
    },
  ]
  </code></pre>
  
  <p>The <code>app.js</code> code checks to see if the returned value type is a function, and if it is it executes it and is supposed to save that to a variable and pass that along to the next function.</p>
  
  <p>I need to use a promise because obviously the bottom function call <code>client.say()</code> will not wait for the returned value form the API call of the function from the <code>commands.js</code> file.</p>
  
  <p>When I try to execute this code, in my terminal I get this error...</p>
  
  <pre><code>TypeError: Cannot read property 'then' of undefined
  at respond (/***/app.js:39:28)
  </code></pre>
  
  <p>So I'm not really sure why this is happening, I'm still learning node and the best ways to do things so maybe I'm on the wrong track here altogether.</p>
  
  <p><strong>Basic functionality</strong></p>
  
  <p>This is a twitch IRC chat bot that will read commands being typed in and respond in chat with a predetermined message. In some cases I will want to do some sort of function before spitting out the message. This is one of those cases. You can see my full <code>app.js</code> code <a href="https://github.com/Jordan4jc/super-fly-twitch-bot/blob/master/app.js" rel="nofollow">here</a> I didn't want to paste the entire thing because it's long and include parts that I thought were not relevant here.</p>