1

How can I read two file using fs and have both result available in a way , where I can compare 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

user1207289
  • 3,060
  • 6
  • 30
  • 66
  • try `fs.readFileSync`, the "Synchronous version of `fs.readFile()`. Returns the contents of the path." – Kelvin Sherlock Feb 06 '18 at 15:54
  • 1
    If you can: https://github.com/jprichardson/node-fs-extra gives a version of `readFile` returning a promise instead of using a callback. Otherwise look at [util.promisify](https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original). – Josh Lee Feb 06 '18 at 15:56

3 Answers3

5

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];
});
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • when I do like above plus this code `var fileDiff = require('diff'); var difference = fileDiff.diffSentences(file1, file2);` or `diffChars` , I get 'TypeError: value.split is not a function' – user1207289 Feb 06 '18 at 16:53
  • so why does `console.log(typeof file1)` and `console.log(typeof file2)` returns a `string` ? – user1207289 Feb 06 '18 at 17:00
  • it worked after I added `.toString()` to `file1` and `file2` . Thanks! – user1207289 Feb 06 '18 at 17:15
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);
  });
});
Moritz Schmitz v. Hülst
  • 3,229
  • 4
  • 36
  • 63
0

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;})
Prajval M
  • 2,298
  • 11
  • 32