bob_cobb
bob_cobb

Reputation: 2269

Want to pass a variable to node async's map method

I'm using node async, and I'd like to pass a variable to the method that it uses within the second parameter... e.g:

  async.map(submissions, addScore, function(err, submissions) {
    if (submissions) {
      return submissions;
    }
  });

I want to pass userId along with addScore but am not sure how to do this.

addScore is my method that I call on every submission and it requires a userId.

Upvotes: 1

Views: 1885

Answers (1)

Ryan Lynch
Ryan Lynch

Reputation: 7776

You can use a closure to put the userId into the addScore function scope:

var createAddScore = function(userId){
    return function(val){
        // do something with userID and val
    }
}

then:

async.map(submissions, createAddScore(1), function(err, submissions) {
    if (submissions) {
        return submissions;
    }
});

Upvotes: 5

Related Questions