DEV Community

Shivang Garg
Shivang Garg

Posted on

Asynchronous Reading of Directory and File in Node.js

Asynchronous programming in JavaScript certainly makes many operations easy to perform though initially, it is tough to get that.

fs.readdir reads the content of the directory and fs.readFile reads the content of the file asynchronously. So, here in this code, we will be reading the directory content and then the files in that directory in an asynchronous way.

Alt Text

Here, we are first reading the directory, storing the content of the directory in fileNames and as soon as directory traversing is done, we then start traversing the files of that directory.

Each file is read and after that, the data of that file is pushed back into the dataset.

If you find anything wrong or any other method for this, do mention in the comment section.

Top comments (2)

Collapse
 
deebeast profile image
Deepak Vishwakarma

You can adopt some functional programming ideology and make your functions return output based on input

Collapse
 
shivang10 profile image
Shivang Garg

Thanks for the suggestion