| by Arround The Web | No comments

How Does fsPromises.open() Work in Node.js?

Node.js “module” system offers a built-in “fs(FileSystem)” module that deals with operating system files in a way of accessing to read, write, delete, update, and rename them. This module comes with many useful methods to perform these particular tasks based on their names and functionalities such as the “fs.open()” method opens the file asynchronously, “fs.openSync()” opens the particular file synchronously, and so on. Moreover, it also supports promise-based operations to make the built-in processes a promise.

This write-up will elaborate on how the “fsPromises.open()” method works in Node.js.

Pre-requisites: Before moving on to the practical implementation of any method, first create a “.js” file of any name and write all the source code to it. In this scenario, we have created an “index.js” file.

What is “fsPromises.open()” in Node.js?

The “fsPromises.open()” is the pre-defined method of the “fs” module that asynchronously opens the file as a promise and returns a “file descriptor”. If the promise is resolved then it verifies that the specified folder is opened otherwise it is rejected with an error object.

The “file descriptor” is simply a non-negative integer index in the file descriptor table. The file descriptor table is an array within the “PCB(Process Control Block)” that keeps the records of all the processes of an operating system.

How Does fsPromises.open() Work in Node.js?

The working of the “fsPromises.open()” method relies on its generalized syntax that is written below:

fsPromises.open( filename, flags, mode)

 

According to the above syntax, the “fsPromises.open()” method works on the following three parameters:

  • filename: It tells the particular file name and the path in the form of a string, URL, or buffer that the user wants to read by default.
  • flags: It may be a string or a non-negative integer value that specifies the operations to perform on the opened file. Its default value is “r” by default.
  • mode: It specifies the file permissions that is “0o666(both readable and writable)” by default.

Return Value: It gives a “promise” as a returned value of the “fsPromises.open()” method.

Example 1: Applying the “fsPromises.open()” Method With Default Flag

This example applies the “fsPromise.open()” method to open the specified file for reading and returning the file descriptor:

const fs = require('fs/promises');
async function myFunc() {
let filehandle;
try {
 filehandle = await fs.open('sampleFile.txt', 'r');
 console.log(filehandle.fd);
 console.log(await filehandle.readFile({ encoding: 'utf8' }));
} finally {
 if (filehandle) await filehandle.close();
 }
}
myFunc();

 

The description of the above-sted code lines is written here:

  • Firstly, import the “fs” module from the “fs/Promises” to use the “async” versions of the “fs” methods.
  • Next, the “async” keyword makes the specified “myFunc()” function a promise.
  • After that, the “let” keyword initializes the “filehandle” variable.
  • Now, the “try” statement defines a code block that first applies the “fs.open()” method with the “await” keyword to open the specified text file for reading by specifying the file name and the “r(read)” flag as its arguments. The “await” keyword will pause the execution of the remaining asynchronous function until the specified is resolved or rejected.
  • The “console.log()” method prints the “fd(file descriptor)” of the specified file that is opened using the “fs.open()” method.
  • The next “console.log()” method uses the asynchronous “readFile()” method to read the given file contents into the “utf8” string format.
  • When the “try” block is executed successfully, then the “finally” code block will run the “if” statement that uses the “close()” method to close the file descriptor asynchronously.
  • Lastly, invoke the “myFunc()” function.

Output

Run the “index.js” file:

node index.js

 

The following output returns the file descriptor and also retrieves the specified file content into the “utf8” string format:

That’s all about the working of the “fsPromises.open()” method in Node.js.

Conclusion

The “fsPromises.open()” method works asynchronously in Node.js to open a file. It is a promise-based method that returns a promise if it is fulfilled or resolved. This method opens the file and performs special operations on it such as read, write, append, and delete. This blog has explained the working of the “fsPromises.open” method in Node.js.

Share Button

Source: linuxhint.com

Leave a Reply