Is this a callback function?

function a(fn){
    console.log(1)
    fn.call()
}
function b(fn){
    console.log(2)
    fn.call()
}

a(()=>{b(()=>{a()})})        

is this a callback function? Why can"t you call a (b (a () in this way? if there is no function called inside the function, isn"t it possible to use it this way a (b ())

Mar.16,2021

I've made it very clear upstairs. Let me emphasize the main point again

() = > {fn ()} is an anonymous function expression, and taking it as a parameter is equivalent to using this anonymous function as a parameter;
fn () is a statement, that is, executing the fn function and returning the execution result. If the code does not have an explicit return , then return undefined . Taking it as a parameter is equivalent to taking the execution result of fn as a parameter

your a , b functions have all parameters that are functions, so you can't use fn () directly, you have to wrap them in a function before you can

. The parameter passed by a () in

a (b ()) is not a b function, but the return value of b function run
if b is to be passed in as a callback function, it should be a (b)


callback function

Menu