title: How do you call a second "external" function using Nodejs, Expressjs and bluebird
body: <p>Im working on building a nodejs (which I'm pretty new to) application.  I've figured out using Promises and am able to pull data in from a database.  you can see the code I'm working with here:</p>
  
  <p>myModel.js</p>
  
  <pre><code>var express = require('express');
  var app = express();
  var Promise = require("bluebird");
  var mysql = require('../helpers/db');
  
  // handle a get request
  exports.post = function(params, callback) 
  {
      function getData() 
      {
          return new Promise(function(resolve, reject) 
          {
              mysql.getResults('select id from someTable',function(err, rows) 
              {
                  resolve(rows);
              });
          });
      }
  
      function getMoreData() 
      {
          return new Promise(function(resolve, reject) 
          {
              mysql.getResults('select * from anotherTable',function(err, rows) 
              {
                  resolve(rows);
              });
          });
      }
  
      getData().then(
          function(data)
          {
              console.log(data); // Log the value once it is resolved
              return data;
          })
      ).then(
          function(d)
          {
              console.log(d);
          }
      ).catch(
          function(err) 
          {
              console.log(err)
          }
      );
  }
  </code></pre>
  
  <p>../helpers/db.js</p>
  
  <pre><code>var mysql = require('mysql');
  var Promise = require('bluebird');
  pool = mysql.createPool({
      connectionLimit: 10,
      database : 'XXXXXX',
      port     : 3306,
      host     : 'localhost',
      user     : 'XXXXXX',
      password : 'XXXXXX'
  });
  
  var executeQuery = Promise.promisify(function executeQuery(query, callback) {
      pool.getConnection(function (err, connection) {
          if (err) {
              return callback(err, null);
          }
          else if (connection) {
              connection.query(query, function (err, rows, fields) {
                  connection.release();
                  if (err) {
                      return callback(err, null);
                  }
                  return callback(null, rows);
              })
          }
          else {
              return callback("No connection", null);
          }
      });
  })
  var getResults = Promise.promisify(function getResult(query,callback) {
    executeQuery(query, function (err, rows) {
       if (!err) {
          callback(null,rows);
       }
       else {
          callback(true,err);
       }
     });
  })
  
  exports.getResults = getResults;
  </code></pre>
  
  <p>This all works fine.  Now what if I want to also pull it data from the function getMoreData?  The goal is be to use data from getData to use in querying the data in getMoreData, and the push the results farther down the "then" chain.</p>
  
  <p>Just having trouble getting my brain around how that would happen.....and I've been banging my head on this all day....</p>
  
  <p>Help is appreciated.</p>
  
  <p>***** EDIT *****</p>
  
  <p>Here's what I have found to work:</p>
  
  <p>myModel.js</p>
  
  <pre><code>var express = require('express');
  var app = express();
  var Promise = require("bluebird");
  var mysql = require('../helpers/db');
  
  // handle a get request
  exports.post = function(params, callback) 
  {
      return new Promise(function(resolve, reject)
      {
          mysql.getResults('select id from sometable order by id', function(err, rows1) 
          {
              // console.log(rows1[0]);
              mysql.getResults('select field1, field2 from someothertable where id=' + rows1[0].id, function(err, rows2) 
              {
                  resolve(rows2);
              });
          })
      }).then(function (params) 
      {
          callback(null, params);
      });
  }
  </code></pre>
  
  <p>../helpers/db.js</p>
  
  <pre><code>var mysql = require('mysql');
  var Promise = require('bluebird');
  pool = mysql.createPool({
      connectionLimit: 10,
      database : 'XXXXX',
      port     : 3306,
      host     : 'localhost',
      user     : 'XXXXX',
      password : 'XXXXX'
  });
  
  var executeQuery = function executeQuery(query, callback) {
      pool.getConnection(function (err, connection) {
          if (err) {
              return callback(err, null);
          }
          else if (connection) {
              connection.query(query, function (err, rows, fields) {
                  connection.release();
                  if (err) {
                      return callback(err, null);
                  }
                  return callback(null, rows);
              })
          }
          else {
              return callback("No connection", null);
          }
      });
  }
  var getResults = function getResult(query,callback) 
  {
      executeQuery(query, function (err, rows) 
      {
          if (!err) 
          {
              callback(null,rows);
          }
          else 
          {
              callback(true,err);
          }
      });
  }
  
  exports.getResults = getResults;
  </code></pre>
  
  <p>Accepted the answer because it got me 90% of the way there.  Using the added promise was the only way to get the second getResults to resolve.  Then I removed the promises from helpers/db, which alleviates the anti-pattern issue.</p>