Await promisified fs.writeFile vs fs.writeFileSync

Viewed 23305

Are there some advantages of one of this options?

1.

const fs = require('fs')

const testFunc1 = async () => {
  fs.writeFileSync('text.txt', 'hello world')
}

2.

const fs = require('fs')
const util = require('util')
const writeFilePromisified = util.promisify(fs.writeFile)

const testFunc2 = async () => {
  await writeFilePromisified('text.txt', 'hello world')
}

I am aware the difference betweeen writeFile and writeFileSync. The question is are there some diffs between promisses that return testFunc1 and testFunc2. So ist it the same to calling testFunc1.then(...) // or await testFunc1 or testFunc2.then(...) // or await testFunc2

These both promisses will be fullfilled when file writing is done.

5 Answers

fs already contains promisified API that doesn't need promisify.

const fsPromises = require("fs/promises");
await fsPromises.writeFile(file, data[, options])

Asynchronous promise-based version requires to use it as a part of promise-based control flow, while synchronous version doesn't impose this requirement.

Asynchronous readFile/writeFile is non-blocking, while synchronous readFileSync/writeFileSync is blocking but allows to complete a job faster. This may be noticeable during intensive IO operations.

To illustrate the difference between two promises that returns functions:

const fs = require('fs')
const util = require('util')

const testFunc1 = async () => {
  fs.writeFileSync('text.txt', 'hello world')
  console.log('file write done with writeFileSync')
}

const writeFilePromisified = util.promisify(fs.writeFile)

const testFunc2 = async () => {
  await writeFilePromisified('text.txt', 'hello world')
  console.log('file write done with promisified writeFile')
}

console.log('start test1')
testFunc1().then(() => {
  console.log('promise 1 is fullfiled')
})
console.log('start test2')
testFunc2().then(() => {
  console.log('promise 2 is fullfiled')
})
console.log('stop')

Output would be:

start test1
file write done with writeFileSync
start test2
stop
promise 1 is fullfiled
file write done with promisified writeFile
promise 2 is fullfiled

So like estus said testFunc1 blocks the execution of the main thread. testFunc2 do not block.

fs.readFile takes a callback function, which means it will not block the execution of your script. fs.readFileSync however does not take a callback, which means that the execution of your script will be paused untill the process is finished. Using promisfy is one way to solve this issue, for small files it wont make a difference, but for large files you might want to transform fs.readFileSync into a promise so you wont block the execution. Hope that helps.

The difference between writeFile() and writeFileSync() is as explained by others that writeFileSync() "blocks". So, what is the difference between "blocking" and "not blocking"?

I wrote a small test where I compared writeFile() and writeFileSync() in terms of speed. I measured the time it took for writeFileSync() to return a result vs. the time it took from calling writeFile() until its callback-argument got called. To my (initial) surprise there was no noticeable difference between the two. writeFile() did not seem any faster than writeFileSync(). So why should I use it when it seems to complicate my program-flow?

But then I measured the time writeFile() took if I didn't wait for its callback-argument to get called. There was a big difference in speed, maybe 10x.

Having a big or no difference thus depends on the rest of your program. If you make a call to writeFileSync() that takes a long time, your program can do nothing else while it is waiting for writeFileSync() to return. If you do writeFile() and wait for the callback to complete before doing anything else, the outcome is no different. But writeFile() is much faster if you don't (need to) wait for its callback to get called.

This would have a big difference if you write a server of some kind that calls writeFileSync(). The server could not service any new requests while it is waiting for a writeFileSync() to complete. And if most requests caused a call to writeFileSync() that could have a big impact on the performance of your server.

fs.writeFileSync( file, data, options )

fs.writeFile( file, data, options, callback)

The Asynchronous function has a callback function as the last parameter which indicates the completion of the asynchronous function.

The ‘fs’ module implements the File I/O operation. Methods in the fs module can be synchronous as well as asynchronous. 

Developers prefer asynchronous methods over synchronous methods as they never block a program during its execution, whereas the later does. Blocking the main thread is "malpractice" in Node.js, thus synchronous functions should only be used for "debugging" or when no other options are available.

The fs.writeFileSync() is a synchronous method & creates a new file if the specified file does not exist while fs.writeFile() is an asynchronous method.

Related