Node.js - Javascript - How to pass variables to a callback function -


i working node.js streams , end in several places:

var myvariable = 'xxx'; ... .on('end', function() {     // myvariable }); 

the behaviour of function same depends on variable refactor to:

var myvariable = 'xxx'; ... .on('end', myfunction);   function myfunction(param){     // param } 

how can pass value of myvariable in call .on('end', myfunction);?

you make factory returns customized functions used callback.

function myfactory(param) {     return function(e) {         console.log( 'param value:', param );         //     } } 

you use this:

.on('end', myfactory(1));  .on('end', myfactory(2)); 

if "end" event passes parameters callback other or in addition event object, add them returned function's parameter list (return function(e, foo, bar) { ...)


Comments