title: How do I keep from losing 'this' context in a JavaScript application?
body: <p>Here is a sample of my code:</p>
  
  <pre><code>function QueueService() {
      var key = Config.get("AWS.accessKeyID");
      var secret = Config.get("AWS.secretKey");
      var credentials = new AWS.Credentials(key, secret, sessionToken = null);
  
      this._sqs = new Promise.promisifyAll(new AWS.SQS({apiVersion: '2012-11-05', region: "us-west-2", endpoint: "sqs.us-west-2.amazonaws.com", credentials: credentials}));
  }
  
  QueueService.prototype.FillBirdQueue = function(birds) {
      var self = this;
  
      birds.forEach(function(bird_batch) {
          var params = {
              Entries: bird_batch,
              QueueUrl: Config.get("AWS-Queue.Birds")
          };
  
          return self._sqs.sendMessageBatchAsync(params);
      });
  };
  </code></pre>
  
  <p>If I omit the <code>var self = this;</code> and call <code>return this._sqs.sendMessageBatchAsync(params);</code> I get an error because <code>_.sqs</code> is undefined. It would appear that <code>this</code> has changed and the only way around it is to save it for later using <code>var self = this;</code>. </p>
  
  <p>I feel like there's a better way of going about this, but I'm not entirely sure how to use the tools at my disposal. I'm currently using Bluebird and Lodash, both of which support binding. In the past, I've passed <code>this</code> by executing the following in Bluebird:</p>
  
  <pre><code>somethingAsync().bind(this).then(function(result){});
  </code></pre>
  
  <p>I haven't been able to use this pattern because I'm not doing anything async at first. I'm only calling one promise and need to be able to access the variable I defined in my constructor to do so. </p>
  
  <p>I'm overlooking something..what can it be?</p>