In FileSystem We Believe

A short recap about fs.readFile(), fs.readFileSync() and fs.createReadStream()

Image for post
Image for post
Photo by Maksym Kaharlytskyi on Unsplash
//FS short for file system, is a powerful built-in module in Node.const fs = require('fs');

fs.readFileSync()

fs.readFileSync( path, options )
let data = fs.readFileSync('./home/file.json', 'utf8');

fs.readFile()

fs.readFile( path, options, cb )
fs.readFile('./home/file.json', 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
});
var fs = require('fs');
function readFileAsSync(){
new Promise((resolve, reject)=>{
fs.readFile(filename, "utf8", function(err, data) {
if (err) throw err;
resolve(data);
});
});
}
async function callRead(){
let data = await readFileAsSync();
console.log(data);
}
callRead();

fs.createReadStream()

fs.createReadStream( path, options)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store