T
T
Timokins2019-12-14 15:22:23
Node.js
Timokins, 2019-12-14 15:22:23

When is fs.openSync preferable to fs.promise.open (and more)?

When are synchronous functions of the fs module preferred over asynchronous functions?
fs.openSync vs fs.promise.open
fs.readSync vs fs.promise.read
etc.
In a case with the server - it is clear. And in the case of conventional utilities - no.
For example, I read the file line by line and put the lines into the database one by one too.
In terms of execution time, the utility for reading a file works faster with fs.*Sync.
But I have absolutely no idea what is better for such a function and what pitfalls can be.
And not only with a similar example, in general, in principle, I would like to understand that when fs.*Sync should be used, and when not.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2019-12-14
@timokins

Asynchronous functions are always preferable - they, as you wrote, do not block the thread, which means the thread will not be idle. When execution time is not important to you (for example, in console utilities), you can neglect speed and use synchronous functions - they are simply easier to read with your eyes and easier to build logical sequences.
Apart from readability, synchronous functions have no advantages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question