1

I'd write the following code to practice function with callback in javascript.

fs = require('fs');

function funcWithCallback(callback) {

  fs.readFile('YouBikeTP.txt', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data.length);
  });
  callback();
}

funcWithCallback(function(){
  console.log("string in callback ")
})

The purpose of the code is try to control the sequence of methods execution. The string "string in callback" should be printed after the length of text file be printed, but when I ran this code the result will be:

>> "string in callback"
>> 91389 //the length of YouBikeTP.txt

which is not the result I expected. should be

 >> 91389 //the length of YouBikeTP.txt
 >> "string in callback"

could anyone tell me why will the callback function been called before funcWithCallback(callback) complete ? Did I misunderstand the meaning of callback function ?

3 Answers 3

4

Change your code to this:

Reason: the function you have defined as a callback in the readFile is an asynchronous callback. It doesn't execute right away, rather it executes when the file loading has completed. Therefore you need to call the main callback function after the console.log for async callback is done.

fs = require('fs');

function funcWithCallback(callback) {

   fs.readFile('YouBikeTP.txt', 'utf8', function (err,data) {
      if (err) {
          return console.log(err);
      }
      console.log(data.length);
      callback(); //calling the main callback after the async callback console logs
   });

}

funcWithCallback(function(){
    console.log("string in callback ")
})
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for replying , I have a further question use the same code above. If I add an IO-bound task (eq. save data to another file or a time consuming loop operation ) in the readFile callback at the previous line of callback() (the line that you commanded), will the callback() execute until the I/O-bound task finish ?
yes @張軒銘 whatever you are trying to do, if its synchronous, it will work fine i.e the callback will execute after that task as per my answer. But if the task you are doing is asynchronous, then you will have to place your main callback inside the callback for the big task that you are trying to accomplish. You can also use promises developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
1

You are not calling callback () inside readFile's own callback, you are just calling it after calling readFile, that then calls its callback function (err,data) only after completing.

Comments

1

you must call the callback() in the fs.readFile callback function.As the callback should be called after asyc function written with the result or error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.