6

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?

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
Sean Lindo
  • 1,387
  • 16
  • 33
  • 4
    Using the second parameter to `forEach` is probably the cleanest way of doing it. See [How to pass context to forEach() anonymous function](http://stackoverflow.com/questions/22814097/how-to-pass-context-to-foreach-anonymous-function). – Qantas 94 Heavy Apr 28 '15 at 12:40
  • 2
    You could use `bind`, but there's nothing wrong with the way you're doing it. – adeneo Apr 28 '15 at 12:40
  • Had to reindent your code... I was stuck for ages thinking you meant the value of `this` was different _before_ and _after_ the `foreach` loop - not during :) – James Thorpe Apr 28 '15 at 12:50
  • 1
    The better question would be: *Why do you `return` promises from `forEach` callback?* What were you actually going to do with them? – Bergi Apr 28 '15 at 13:00
  • Why are you doing `new Promise.promsiifyAll` -_-? – Benjamin Gruenbaum Apr 28 '15 at 13:52
  • Did you mean to use `Promise.each` or `Promise.map` instead of `forEach`? – Benjamin Gruenbaum Apr 28 '15 at 13:53
  • @BenjaminGruenbaum Would you be kind enough to explain my error, sir? Re: ```new Promise.promisfyAll``` – Sean Lindo Apr 28 '15 at 15:08
  • @JamesThorpe Thanks for helping out. This wasn't a good morning apparently, I completely lost track of where I was and realized that I was inside of a loop. Sorry all! – Sean Lindo Apr 28 '15 at 15:10
  • @sean feel free to come to chat at http://chat.stackoverflow.com/rooms/17/javascript – Benjamin Gruenbaum Apr 28 '15 at 16:25

0 Answers0