Skip to content

Instantly share code, notes, and snippets.

@mnyamor
Created October 30, 2016 23:03
Show Gist options
  • Select an option

  • Save mnyamor/36b8ae338eb0a0428d91f1ce5d8995cb to your computer and use it in GitHub Desktop.

Select an option

Save mnyamor/36b8ae338eb0a0428d91f1ce5d8995cb to your computer and use it in GitHub Desktop.
Listing Directory Files
/*
- When we use any methods of the fs module we are given the option to use them synchronously or asynchronously.
- reading files synchronously will block the single Node.js thread so all other connections will wait for this synchronous recal
*/
//include the file system module - we can do just about anything with files and directories
var fs = require('fs');
// create var for files ..
// var files = fs.readdirSync('./lib'); //READ FILES FROM LIBRARY FOLDER..
// we're going to synchronously read the lib directory
// console.log(files);
/* You want to take advantage of Node.js's asynchronous nature and not read the files from the directory synchronously.
We can do that simply by dropping the Sync.
Readdir is not going to return our files any longer, but this is an asynchronous command,
so what it's going to do is put in a request to read the files from the library folder
and when the file system is finished reading those files this call back will be invoked.
*/
fs.readdir('./lib', function(err, files) {
if (err) {
throw err;
}
console.log(files);
});
console.log("Reading Files...");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment