Reading file's contents async
We can also perform filesystem operations asynchronously, as the fs module gives us this opportunity. So we will write the same code as before, but in this case we are NOT going to block our whole script from going further.
This is very efficient since node.js is a single-threaded runtime environment and there is no possibility to split a script into different threads in order to achieve parallelism and perform multiple tasks simultaneously.
The way we would achieve the same effect with node.js is by performing asynchronous operations whenever possible.
That's why we will use the fs.readFile that normally accepts a callback as a second argument. Now because we don't want to mess with callback hell, or the pyramid of doom we will use the promisified way of these functions through the fs.promises.
Fs.promises is a collection of asynchronous filesystem functions, but instead of implemented with the callback way, the have been wrapped inside promises. So we can use them inside an async/await function as it is shown above.
If you are not sure, regarding the difference between async/await, callbacks and promise.then syntax, then it would be better to read again the asynchronous chapter of the vanillaJS, where these topics are thoroughly explained.
Attention: Async/await and the fs.promises module are stable while we are writing the current lines (for node.js versin 10 and after). Be careful if you are using an older node.js version (before 10, you will have to use the util.promisify module instead of fs.promise) and before node.js version 7.6 the .then syntax in favor of async/await.