title: Promises in ES6 Classes
body: <p>I'm trying to write a class that has methods that return promises and promise chains. This attempt returns error from do_that() </p>
  
  <p>I understand the problem with using 'this' which is why I used the self=this kludge, but I still get an error.</p>
  
  <blockquote>
    <p>TypeError: Cannot read property 'name' of undefined.</p>
  </blockquote>
  
  <p>In addition to the question, how do I fix this, is there a cleaner way to do this?</p>
  
  <pre><code>var Promise = require('bluebird');
  
      class myClass {
  
          constructor(name, pipeline) {
              this.name = name;
          }
  
          do_this() {
              var self = this;    // &lt;-- yuck. Do I need this?
              return new Promise(function(resolve, reject) {
                  setTimeout(function () { console.log("did this " + self.name);  resolve(self);  }, 1000);
              })
          }
  
          do_that() {
              var self = this;    // &lt;-- yuck
              return new Promise(function(resolve, reject) {
                  setTimeout(function () { console.log("did that " + self.name);  resolve(self);  }, 1000);
              })
          }
  
          do_both() {
              return this.do_this().then(this.do_that);
          }
  
      }
  
      new myClass("myobj").do_both();  // &lt;-- TypeError: Cannot read property 'name' of undefined.
  </code></pre>