Skip to content
Related Articles

Related Articles

NodeJS fs.fsyncSync() Method
  • Last Updated : 08 Feb, 2021

In this article, we will be learning about fsyncSync() method in NodeJS. Before diving deep into the topic, let’s have a brief idea about what a fsync() method is.

Node.js provides us with a ‘fs’ module that helps us with both synchronous and asynchronous forms. An asynchronous form has a callback as the last argument whereas in case of synchronous, it only consists of the file descriptor. A fsync() function does not return any value but helps to get the file descriptor in a synchronous way. A fsyncSync() method is just the synchronized form of fsync(). It helps to synchronize the disc cache.

Syntax:

fs.fsyncSync(fd)

fd refers to the File Descriptor and its return value is undefined.

Parameter: File Descriptor.



Return Type: Undefined.

File descriptor is a number that uniquely identifies an open file in a computer. It provides an entry to the global file table that provides us with the location of that entry. Example : if the file descriptor is 3, it means that in the global file table it is saved as a read/write operation with offset : 12.

At first, we need to install ‘fs’ and ‘express’ module in our NodeJS project. 

npm install fs express --save

Create a file ‘example.txt’ inside your node.js project so that we can use any kind of operation for that file. After that write the necessary javascript code for our project.

example.txt




const fs = require('fs'); 
const express = require('express'); 
const app = express(); 
  
// Retuns the file descriptor
const fd = fs.openSync('example.txt', 'r+'); 
  
// Function call 
fs.fsync(fd, (err) => { 
    if(err) { 
        console.log(err) 
    } else {  
        console.log("FD:",fd) 
    
}); 
console.log(fs.fsyncSync(fd));   
  
// Server setup 
app.listen(3000, function(error){ 
    if (error) console.log("Error"
    console.log("Server running to port 3000"
})
node app.js

OUTPUT


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :