Here is an example 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 var self = this; and call return this._sqs.sendMessageBatchAsync(params); I get an error because _.sqs is undefined. This seems to have changed, and the only way is to save it for future use var self = this; .
I feel that there is a better way around this, but I'm not quite sure how to use the tools at my disposal. I am currently using Bluebird and Lodash, both of which support binding. I used to go through this by doing the following in Bluebird:
somethingAsync().bind(this).then(function(result){});
I could not use this template because at first I did nothing async. I offer only one promise and must have access to the variable defined in my constructor for this.
I'm missing something. What could it be?
source share