Open In App

Node.js fs.fsync() Method

Improve
Improve
Like Article
Like
Save
Share
Report

In Node, the ‘fs’ module provides an API for interacting with the file system in a manner closely modeled around standard Portable Operating System Interface (POSIX) functions.

It has both synchronous and asynchronous forms. The asynchronous form always takes a completion callback as its last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be null or undefined. The fs.fsync() method is a kind of asynchronous form. Synchronizes a file with the one stored on the computer.

Syntax:

fs.fsync(fd, callback);

Parameters: This method accepts two parameters as mentioned above and described below:

  • fd: It is a file descriptor (integer) to get in a synchronous way.
  • callback: It is a callback function to check whether any error has occurred.

Return Value: This function does not return any value.

Example 1: Filename: index.js




// Requiring module
const fs = require('fs');
   
// Opening a file
const fd = fs.openSync('example.txt', 'r+');
   
// Function call
fs.fsync(fd, (err) => {
    if(err) {
        console.log(err);
    } else {
        console.log("FD:",fd);
    }
})


Output:

FD: 3

Example 2: Filename: index.js




// Requiring modules
const fs = require('fs');
const express = require('express');
const app = express();
   
const fd = fs.openSync('example.txt', 'r+');
  
app.get('/', (req, res) =>{
});
  
// Function call
fs.fsync(fd, (err) => {
    if(err) {
        console.log(err)
    } else
        console.log("FD:",fd)
    }
});
   
// Server setup
app.listen(3000, function(error){
    if (error) console.log("Error")
    console.log("Server listening to port 3000")
})


Run index.js file using the following command:

node index.js

Output:

Server listening to port 3000
FD: 3

Reference: https://nodejs.org/api/fs.html#fs_fs_fsync_fd_callback



Last Updated : 21 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads