Good morning StackOverflow community!
My question is very theoretical this time, I am studying the methods readFile()
and readFileSync()
of the module fs
.
Being the main advantage of nodejs the fact of executing asynchronous operations, I am left wondering why and what it exists for readFileSync()
? Isn't it true that you could treat all cases with readFile()
?
This approach leads me to my most important question: There are cases where it is more convenient to use readFileSync()
instead of readFile()
?
Thanks in advance for your collaboration!
I can think of the following use cases:
In conclusion, before having tools like async/await, introducing asynchronous code complicates the code, makes it less readable and can lead to errors that are difficult to debug. I don't think there is a situation where the synchronous version is better, but sometimes it is more convenient.
Oh, and when you're learning node and you don't get soaked in callbacks, it's the easy way that we've all taken as newbies haha