Kulin Kulin - 11 days ago 6
Javascript Question

Add argument to callback

I only need add element to a callback, but I don't know the way.

I use a nodejs module, when event is listened, execute callback 'mrktEvent' function:

session.sub('Hello', mrktEvent);


In mrktEvent function I receive 2 arguments:

function marketEvent (args,kwargs) {}


I need add one more argument for get it in the function like this (for example):

session.sub('Hello', mrktEvent("newArg"));
function marketEvent (args,kwargs,newArg) {}

str str
Answer

Then just create a wrapper callback:

session.sub('Hello', function(args, kwargs) {
    return marketEvent(args, kwargs, 'newArg');
});

Or using ES6 arrow functions:

session.sub('Hello', (args, kwargs) => marketEvent(args, kwargs, 'newArg'));