title: Accessing 'this' of an object inside promise callback (then)
body: <p>I want to create an object in Javascript. </p>
  
  <p>One of the methods should execute a promise chain. Each of the methods in the chain have to access a config variable that is a member of the object.
  The problem is, the <code>this</code> operator is changed in <code>PromiseMethod2</code> and I can't access the config variable (It works correctly in <code>PromiseMethod1</code>).</p>
  
  <p>Here's my code:</p>
  
  <pre><code>var SomeObject(config) {
      var that = this;
      that.config = config;
  }
  
  SomeObject.prototype.SomeMethod = function() {
      var that = this;
  
      that.PromiseMethod1()
        .then(that.PromiseMethod2)
        .catch(console.error);
      }
  
  SomeObject.prototype.PromiseMethod1 = function() {
      var that = this;
      config = that.config;
  
      return SomePromise();
  }
  
  SomeObject.prototype.PromiseMethod2 = function(someParams) {
      var that = this;
      config = that.config;
      params = someParams;
  
      return SomePromise();
  }
  
  
  var someObject = new SomeObject(someConfig);
  someObject.SomeMethod().then(function () {
      console.log('Done!');
  }
  </code></pre>
  
  <p>I want to use the method delegate in the chain instead of just executing:</p>
  
  <pre><code> that.PromiseMethod1().then(function(response) { return that.PromiseMethod2(that, response); };
  </code></pre>
  
  <p>I can't use the <code>bind</code> method because it looks like it gets rebinded when the callback is executed.</p>
  
  <p>Is there a solution to this?
  Why's there a difference between <code>PromiseMethod1</code> and <code>PromiseMethod2</code>?</p>