Is it possible to pass additional arguments to async.eachSeries

is it possible to pass additional arguments or parameters to async.EachSeries

The method signature is : EachSeries(arr, iterator, callback)

and I have a method that merges email-recipients with a mail template async

var mergeTemplate = function(template,recipients,callback){

  async.EachSeries(recipients,processMails,callback);

};

var processMails = function(template,singleRecipient,callback){
   //...this would contain an async.waterfall of tasks to process the mail
   async.waterfall(tasks,callback);
}

What I would need is to pass through the template without using a "dirty" global variable... Is this possible and if so, how?

Thanks

You can use .bind to pass in template without using a global variable:

var mergeTemplate = function(template, recipients, callback){
    async.eachSeries(recipients, processMails.bind(processMails, template), callback);
};

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

So processMails.bind(processMails, template) creates a new function with this set to processMails and the first argument to this new function is template.

This is equivalent (but less verbose) to calling processMails directly like this:

var mergeTemplate = function(template, recipients, callback){
    async.eachSeries(
      recipients, 
      function(){
         return processMails(template);
      }, 
      callback);
};