Javascript callback function with parameters [duplicate]

In direct answer to your question, this does not work:

 firstfunction(callbackfunction(param));

That will execute callbackfunction immediately and pass the return value from executing it as the argument to firstfunction which is unlikely what you want.


It is unclear from your question whether you should just change firstfunction() to pass two parameters to callbackfunction() when it calls the callback or whether you should make an anonymous function that calls the callback function with arguments.

These two options would look like this:

function firstfunction(callback) {
    // code here
    callback(arg1, arg2);
}

firstfunction(callbackfunction);

or

function firstfunction(callback) {
    // code here
    callback();
}

firstfunction(function() {
    callbackfunction(xxx, yyy);
});

Leave a Comment