16

Out of curiosity, i want to know if there is any difference between the two.

readFileSync:

function parseFile(filePath) {
  let data = fs.readFileSync(filePath);
}

readFile with promisify:

const readFilePromise = promisify(fs.readFile);
async function parseFile(filePath) {
  let data = await readFilePromise(filePath);
}

If you need some context, im trying to read a bunch of files in a folder, replace a lot of values in each one, and write it again.

I don`t know if there is any difference in using Asyncronous or Synchronous code for these actions.

Full code:

function parseFile(filePath) {
  let data = fs.readFileSync(filePath);
  let originalData = data.toString();
  let newData = replaceAll(originalData);

  return fs.writeFileSync(filePath, newData);
}

function readFiles(dirPath) {
  let dir = path.join(__dirname, dirPath);
  let files = fs.readdirSync(dir); // gives all the files
  files.map(file => parseFile(path.join(dir, file)));
}

function replaceAll(text) {
  text = text.replace(/a/g, 'b');
  return text;
}

readFiles('/files');
  • Why it got downvoted..? Even the answer – Patrick Passarella Dec 11 '18 at 18:33
  • 4
    Not sure about the dv. I think it's pretty rude to downvote with no comment. Especially new contributors. I'll pass on an upvote to help. – Mark Dec 11 '18 at 18:35
  • This question is extremely broad and is likely the cause of a downvote (speculation). Synchronous code executes in a single stack frame, asynchronous code is relinquished to the event queue. You should understand how the stack and event loop work together to understand the difference between synchronous and asynchronous code execution in javascript. I generally recommend watching [this video](https://www.youtube.com/watch?v=8aGhZQkoFbQ) to understand these concepts. – Jake Holzinger Dec 11 '18 at 21:10
  • But i actually know how async vs sync works, i wanted to know the difference especifically between these 2 fs methods, if there was any difference. – Patrick Passarella Dec 12 '18 at 15:34

1 Answers1

11

There's a big difference between the async and synchronous code. Whether that difference matters depends on what you are trying to do. Your javascript is singe threaded, so while you are reading a potentially large file synchronously with fs.readFileSync you can't do anything else such as respond to incoming requests.

If you are running a busy server this can cause big problems because requests queue up while you are reading the file and you may never catch up.

With the async method the file read happens outside your code and it calls your code back when it's done. While it's doing this your code is free to respond to other requests.

If you are just trying to read a local file and it doesn't matter if the thread blocks, then you can use either.

Mark
  • 90,562
  • 7
  • 108
  • 148
  • Ahh okay, i got it now. But fs.readFile is already asynchronous right? So using async/await on top of it doens`t change anything other than making it returns a Promise? – Patrick Passarella Dec 11 '18 at 18:30
  • Correct, `fs.readFile` is async, but it's often more convenient to work with promises or async/await that using callbacks which is why you might promisify it. – Mark Dec 11 '18 at 18:32
  • Does this benefit of async over sync functions still hold true when you tack on an async/await to the promisified readFile() function? – shawn98ag Nov 13 '19 at 22:01
  • @shawn98ag using `async/await` is still using promises, so you still get the benefits of asynchronous code. In other words using `async/await` does not block the thread. – Mark Nov 14 '19 at 03:03
  • 1
    "@shawn98ag using async/await is still using promises, so you still get the benefits of asynchronous code. In other words using async/await does not block the thread." . . This is the crux of what I wanted to know in this question, thanks for the answer/comment, and I think is a perfectly valid question. Why it was downvoted is beyond me, especially when the question clearly states that in the async approach, an `await` is being used. – ako977 Sep 22 '21 at 20:31