Simple nodejs callback example with fs.readFile

后端 未结 3 1660
予麋鹿
予麋鹿 2021-02-11 05:55

I\'m trying to learn async programming and was struggling with lesson 4 of nodeschool.io with the implementation of an async io with callbacks.

Basically, I\'m trying to

3条回答
  •  攒了一身酷
    2021-02-11 06:41

    Same problem for me. This is my solution.

    var fs = require('fs');
    var file = process.argv[2];
    
    function count(pFile, callback) {
      fs.readFile(pFile, "utf8", function(err, data){
         callback(data.split("\n").length-1);
      });
    }
    
    count(file, console.log);
    

提交回复
热议问题