What exactly is the principle of js callback?

learn from a novice in node.js and want to know how callbacks work.
for example, the code for this asynchronous callback:


function abc(err, data){
  console.log(data.toString());
}

require("fs").readFile(filename,abc);

how is this err, data passed to abc? Why can parameters be passed like this? If you want to customize these parameters, what do you need to do to write a callback function yourself?

I want to understand these specific principles here and the process of passing values.

the other thing is that the conditions for asynchronous use are confusing.
for example, I want to pass pathname to router to handle routing during http.createServer, and the routing process calls the function to read out the corresponding .txt file and write it to the browser through response. What do you need to use asynchrony? Which processes can be used with Synchronize?

Mar.20,2021

function dosomething(val,callback){
    try{
        var data = val*val*val //
        callback(false,data)
    }catch(error){
        callback(error,null)
    }
}

function abc(err, data){
  if(err) console.log(err) return
  console.log(data.toString());
}

dosomething(100,abc)
Menu