Fs module in node js (Asynchronous)
Fs module in nodejs (A synchronous version)
I would like to take you through the basics to understand asynchronous version of fs module. Let's see what are parameters & arguments.Eg :function add (a,b){ return a+b; } Here, a & b are called parameters.
Eg : add (5,6) ; //calling a function Here 5 & 6 are arguments.
So, in short parameters are given while defining the function & arguments are given while calling the function.
So we have understood parameter & arguments. Next, we will see what are higher order functions & callbacks.
When we pass a function as parameter to another function, the second function is called as a higher order function. The function passed as a parameter is called callback function. Now let's see how to use fs module in asynchronous version. In order to use modules in nodejs, we need to require it first.
const fs = require ("fs");
Let's Start playing with fs module.
To Create a new folder fs.mkdir("Folder Name " ,() => { console. log ("Folder Created "); });
The main difference between Synchronous and asynchronous is that we need to give a callback function in asynchronous version. The callback function here is an arrow function. The second difference is we don't need to write 'Sync' everywhere as we wrote in asynchronous version. I have already explained synchronous version in my last blog I will provide the link below.
To Create a new file file fs.writeFile("Filename" ,"Text to be written in file",()=>{ console.log("File created"); });
great work
ReplyDeleteWell done dude
ReplyDeletevery nice for begginers .
ReplyDeleteWell done 👍👍👌
ReplyDeleteGreat work 👍
ReplyDeleteAmazing!
ReplyDeletegreat
ReplyDeleteclearly explained and well derived
ReplyDeleteGreat
ReplyDeletegreat work mate
ReplyDelete