Questions about callback principle in some functions of Nodejs

recently, when I was learning nodejs, I saw that many functions are followed by a callback function. For example, I will give an inappropriate example and illustrate my doubts

for example, if there is such a code, I will rewrite it:

  readFile("abc",(err,files) => {
    if(err) throw err
    else {
      console.log(files) // readFilefalse,
    }
  })

for example, this function exposes the annotation function above the api implementation, then I am very good at how the odd source code will be defined. I also tried to write

myself.
   function readFile(path,callback) {
     let flag = false
     if(path) {
       flag = true // flagtrueerrfalse
     }
     .... // 
   }

ask the great god to help solve the confusion, or it may be that my JS is not very solid. No, no, no. It"s a little messy, thank you

Feb.09,2022

if you only meet the requirements of your first comment, write as follows

  

can't write down. In fact, readfile in node is implemented by cPP and calls libuv's uv_fs_read. This function can be set up callback, and at the bottom is libuv, where threads are blocked and other files are read

.
Menu