Open In App

Node.js fsPromises.open() Method

The fsPromises.open() method is used to asynchronously open a file that returns a Promise that, when resolved, yields a FileHandle object.

Syntax:



fsPromises.open( filename, flags, mode)

Parameter: This method accepts three parameters as mentioned above and described below:

Return Value: It returns the Promise.



Example: The below example illustrates the fsPromises.open() method in Node.js:




// Node.js program to demonstrate the   
// fsPromises.open() Method
 
// Include the fs module
const fs = require('fs');
const fsPromises = fs.promises;
 
// Open file Demo.txt in read mode
fsPromises.open('Demo.txt', 'r')
    .then((result) => {
        console.log(result);
    })
    .catch((error) => {
        console.log(error);
    });

Output:

FileHandle { [Symbol(handle)]: FileHandle { fd: 3 } }

Explanation: The file is opened and the flag is set to read mode. After the opening of the file function is called to read the contents of the file and store them in memory.

Note: mode sets the file mode (permission and sticky bits), but only if the file was created.

Some characters (< >: ” / \ | ? *) are reserved under Windows as documented by Naming Files, Paths, and Namespaces.

Article Tags :