javascript - Adding new first argument, then applying original arguments -


i using map map set of functions so:

function runwaterfall(consumer, fns, cb) {      fns = fns.map(function (fn) {         return function () {             fn(consumer, arguments);         }     });      async.waterfall(fns, cb);  } 

so can see, add "consumer" first argument every function fn, don't believe syntax right.

perhaps better:

function runwaterfall(consumer, fns, cb) {      fns = fns.map(function (fn) {         return function () {             fn.apply(null,[consumer, arguments]);         }     });      async.waterfall(fns, cb);  } 

i doubt either of these correct

you can use function.prototype.bind not set this context, provide arguments. close apply, call function while bind return function argument pre-filled:

fns = fns.map(function (fn) {     return fn.bind( null, consumer ) }); 

https://jsfiddle.net/qmuv8com/


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -