Skip to content Skip to sidebar Skip to footer

Callback Function Is Not Working In JavaScript Example

What's wrong in the following callback function example? I am passing some parameters and in the end, I am passing a function that must automatically run when the other tasks are d

Solution 1:

Here is what you want:

function alpha(a, b, fn) {
    console.log(a, b, a + b);
    fn();
}
alpha(5, 10, () => {
    console.log("hello");
});

// or defined by default
function alpha2(a, b, fn = () => {
    console.log("hello");
}) {
    console.log(a, b, a + b);
    fn();
}
alpha2(5, 10);

Solution 2:

You might be looking for something like this, with passing parameters to callback function:

function alpha(a, b, f = (a,b) => a+b) {
    return f(a,b);
}

const multiply = (a,b) => a*b
console.log(alpha(5, 10));
console.log(alpha(5, 10, multiply));

Post a Comment for "Callback Function Is Not Working In JavaScript Example"