newbie approach: what is a javascript callback function?

前端 未结 7 2156
自闭症患者
自闭症患者 2020-11-28 12:49

Is just a function that executes after one other function that calls it, finishes?

Please I know (almost) nothing about programming, and I find it quite hard to find

7条回答
  •  渐次进展
    2020-11-28 13:38

    Code like this

    var result = db.query('select  * from T'); //use result - Standard function
    

    your software is doing nothing and you are just waiting for the database to respond. this somehow either blocks the entire process or implies multiple execution stacks. But a line of code like this

    db.query('select * from T',function(result){
        //use result - Callback function
    });
    

    allow the program to return to the event loop immediately.

    In this execution, server makes that request and continue doing other things, when the request comes back(after millions of clock cycles), you can execute the callback, all you need is the pointer to the callback.

提交回复
热议问题