javascript - node - fs - reading two files so they can be compared - Stack Overflow

How can I read two file using fs and have both result available in a way , where I can pare them. I loo

How can I read two file using fs and have both result available in a way , where I can pare them. I looked at this but its slightly different and I couldnt find a way to do what I need.

I can call the diffChars from callback, but how to do with two callback functions?

  fs.readFile('/abc1.txt',  function (err, data1) {
    console.log(data1);
});
  fs.readFile('/abc1.txt',  function (err, data2) {
    console.log(data2);
});

 later I want to do like this

 var fileDiff = require("diff");
 var difference = fileDiff.diffChars(data1,data2);
 cnsole.log(difference);

note: I am restrictive on libraries I can use because of npm proxy repository

How can I read two file using fs and have both result available in a way , where I can pare them. I looked at this but its slightly different and I couldnt find a way to do what I need.

I can call the diffChars from callback, but how to do with two callback functions?

  fs.readFile('/abc1.txt',  function (err, data1) {
    console.log(data1);
});
  fs.readFile('/abc1.txt',  function (err, data2) {
    console.log(data2);
});

 later I want to do like this

 var fileDiff = require("diff");
 var difference = fileDiff.diffChars(data1,data2);
 cnsole.log(difference);

note: I am restrictive on libraries I can use because of npm proxy repository

Share Improve this question asked Feb 6, 2018 at 15:50 user1207289user1207289 3,2736 gold badges37 silver badges74 bronze badges 2
  • try fs.readFileSync, the "Synchronous version of fs.readFile(). Returns the contents of the path." – Kelvin Sherlock Commented Feb 6, 2018 at 15:54
  • 1 If you can: github./jprichardson/node-fs-extra gives a version of readFile returning a promise instead of using a callback. Otherwise look at util.promisify. – Josh Lee Commented Feb 6, 2018 at 15:56
Add a ment  | 

3 Answers 3

Reset to default 6

This is a perfect case for a Promise and Promise.all.

function readFile(name) {
    return new Promise((resolve, reject) =>
        fs.readFile(name,  function (err, data) {
            if (err) { reject(err); }
            resolve(data);
        });
    });
}

Promise.all(readFile('file1'), readFile('file2')).then(data => {
   var file1 = data[0];
   var file2 = data[1];
});

You don't need the callback functions. You could use fs.readFileSync().

EDIT However, fs.readFileSync() is blocking and the next line will only be executed once the function returns.

If you insist on using callbacks:

fs.readFile("abc123.txt", (error1, data1) => {
  if (error1) {
    return;
  }

  fs.readFile("abc456.txt", (error2, data2) => {
    if (error2) {
      return;
    }

    console.log(data1 === data2);
  });
});

Node uses asynchronous style of coding. So when you are reading file one the 2nd file is also being read and also the next code is being executed. Thus you can use promises or callbacks. Here is one solution

fs.readFile('/abc1.txt',  function (err, data1) {
    console.log(data1);
    fs.readFile('/abc1.txt',  function (err, data2) {
        console.log(data2);
        var fileDiff = require("diff");
        var difference = fileDiff.diffChars(data1,data2);
        console.log(difference);
    });      
});

or use promises like

var data;
fs.readFile('/abc1.txt')
.then((data1)=>{
    data = data1;
    return(fs.readFile('/abc1.txt'))
})
.then((data2)=>{
    var difference = fileDiff.diffChars(data1,data2);
    return(difference)
})
/* do what ever you want here*/
.catch((err)=>{throw err;})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745485070a4629728.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信