Here is a sample of my 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);
});
};
If I omit the var self = this;
and call return this._sqs.sendMessageBatchAsync(params);
I get an error because _.sqs
is undefined. It would appear that this
has changed and the only way around it is to save it for later using var self = this;
.
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 this
by executing the following in Bluebird:
somethingAsync().bind(this).then(function(result){});
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.
I'm overlooking something..what can it be?