title: ES6: How to return promise from inside then block
body: <p>I am hashing a password with a function that returns a promise. I am trying to use the result with <code>node-sqlite3</code>, which has a <code>db.run()</code> method that accepts a SQL command, an array of parameters, and an optional callback. I tried something like the following:</p>
  
  <pre><code>var maybeError = hashPassword("foo")
    .then(function(hashed) {
      return new Promise(function(resolve, reject) { 
        db.run(
          "insert into blah hashed value ?",
          [hashed], 
          function(err) {
            if(err) reject(err); else resolve();
          }
        );
      });
    })
    .catch(//something else);
  </code></pre>
  
  <p>This idea works when not wrapped in the <code>.then()</code> block of the <code>hashPassword()</code> function. I want to export the <code>maybeError</code> Promise to a separate <code>server.js</code> file, which will use it to generate a HTTP status codes, etc.</p>
  
  <h2>Edit:</h2>
  
  <p>The comments correctly identify an error in the orignal <a href="https://plnkr.co/edit/n3cFWmhXtJq4QCz4lHSh?p=preview" rel="nofollow">Plunk</a> that was an artifact of adapting the code to highlight the problem I was having.</p>
  
  <p>Having corrected that error, I think there is still an error as shown in this <a href="https://plnkr.co/edit/qkf4GixVcfqJCRUZJx7l?p=preview" rel="nofollow">updated Plunk</a>.</p>
  
  <pre><code>// Simulate a database a la node-sqlite3
  var fakeDb = function(sql, params, cb) {
    setTimeout(cb("Error"), 500);
  };
  
  // Test that the fakeDB() function works by itself
  var noPromise = function(sql, params) {
    fakeDb(sql, params, function(e) {
      document.write(e);
    });
  };
  
  noPromise("foo", "bar");
  
  // Wrap fakeDB() callback in single promise, that works
  var singlePromise = function(sql, params) {
    return new Promise(function(resolve, reject) {
      fakeDb(sql, params, function(e) {
        if (e) {
          reject(e);
        } else {
          resolve();
        }
      });
    });
  };
  
  singlePromise("foo", "bar")
    .catch(function(e) {
      document.write(e);
    });
  
  
  // Simulate a password hashing function that returns a promise
  var hashPassword = function() {
    return new Promise(function(resolve, reject) {
      resolve();
    });
  };
  
  // Verify that the fake password hashing function works
  hashPassword()
    .then(document.write("foo"));
  
  /* Now attempt to return a promise from the fakeDB function,
   * but this time wrap that inside the then() block of hashPassword.
   * This doesn't work.
   */
  var nestedPromise = hashPassword()
    .then(function(sql, params) {
      return new Promise(function(resolve, reject) {
        fakeDb(sql, params, function(e) {
          if (e) {
            reject(e);
          } else {
            resolve();
          }
        });
      });
    });
  
  nestedPromise("foo", "bar") // &lt;== This was the problem: should not have arguments.
    .catch(function(e) {
      document.write(e);
    })
    .then(function(x) {
      document.write(x);
    });
  </code></pre>
  
  <p>This writes <code>ErrorfooError</code> but not <code>ErrorfooErrorError</code> to the document. That <code>return</code> in the <code>nestedPromise()</code> function does not appear to be working.</p>
  
  <p>Thanks!</p>