The fs(File System) module enables interacting with the file system in a way modeled on standard POSIX functions, which means we can perform I/O operations with our Computer’s file System. Like reading data from a file, writing data to a file, etc. All the file system operations have synchronous and asynchronous forms and mostly the asynchronous form takes a completion callback as its last argument.
The fs.fdatasyncSync() (Added in v0.1.96) method is the synchronous version of fs.fdatasync().
fs.fdatasync() method is an inbuilt API(Application Programming Interface) of the fs(File System) module which is similar to fs.fsync() method, fs.fsync() transfers ( or flushes) all modified data of the file to the disk memory so that all changed information can be retrieved even if the system crashes or rebooted, but fdatasync() method does not transfer or flush modified metadata unless that metadata is needed in order to allow a succeeding data write/read to be correctly handled.
Both fdatasyncSync() and fdatasync() methods reduce unnecessary disk activity.
Syntax:
// Require fs module at the top of this .js file const fs = require('fs'); fs.fdatasyncSync(fd);
Parameter: fs.fdatasyncSync() method takes only one parameter.
- fd<integer>: This an integer type value.
Example: In the main.js file, write the following code.
// Node.js program to demonstrate the // fs.fdatasyncSync() method // Using require to access fs module const fs = require( 'fs' );
// Data function which we'll write to data.js function data() {
console.log( "Hi this is data function" );
} // Open the file fs.open('data.js ', "a+", (err, fd) => {
if (err)
throw err;
// Write our data
fs.writeFile(fd, data, (err) => {
// Checking error
if (err)
throw err;
// Force the file to be flushed
fs.fdatasyncSync(fd); //return undefined
// Print after dataSync
console.log("Writing ' data ' in ' data.js'... ")
});
}); |
Run the file using node main.js
Output:
Writing 'data' in 'data.js'
Reference:https://nodejs.org/api/fs.html#fs_fs_fdatasyncsync_fd