what is the difference between fs.copyFile() and fs.copyFileSync() in node.js
fs.copyFile("a.txt", "b.txt", (err) => {
if (err) throw err;
console.log("file is copied to the destination");
});
what is the difference between fs.copyFile() and fs.copyFileSync() in node.js
fs.copyFile("a.txt", "b.txt", (err) => {
if (err) throw err;
console.log("file is copied to the destination");
});
fs.copyFile is used for asynchronous process whereas fs.copyFileSync is used for synchronous process.
Example of fs.copyFile (asynchronous) :
In this the tasks which gets completed first will give output.
Task - 1 : (fs.copyFile) takes 5s to complete.
Task - 2 : (fs.copyFile) takes 6s to complete.
Task - 3 : (fs.copyFile) takes 1s to complete.
Output :
Task - 3 -> Task - 1 -> Task - 2
Example of fs.copyFileSync (synchronous) :
In this order will be maintained. Like After completion of first task then it will go for next task and so on.
Task - 1 : (fs.copyFileSync) takes 5s to complete.
Task - 2 : (fs.copyFileSync) takes 6s to complete.
Task - 3 :(fs.copyFileSync) takes 1s to complete.
Output :
Task - 1 -> Task - 2 -> Task - 3