Lab 1 OSD600

Thanks for joining me!

The node.js module feature that I will be talking about in this blog post is fs.exists(path, callback) and fs.existsSync(path).

Feature: fs.exists(path,callback)

This feature is used to test whether or not the given path exists by checking with the file system. If the path does not exists it calls the callback argument with either true or false.

Screenshot (53)

The fs.exists() callback has only one boolean parameter, which is why fs.access() is recommended instead of fs.exists().

It introduces a rare condition when fs.exists() is used to check if a file exists before calling fs.open.

Feature: fs.existsSync(path)

This is a synchronous version of fs.exists(). Returns true is if the file exists, false otherwise.

Difference between synchronous or asynchronous: An asynchronous function is a code block within the function that is mostly non-blocking to the end user and user will not be prevented to perform different actions for various sub-processes. Synchronous is the opposite of asynchronous and is never recommended.

References:

https://www.javacodegeeks.com/2013/12/node-js-synchronous-and-asynchronous-functions.html

http://nodejs.cn/doc/node/fs.html#fs_fs_exists_path_callback

https://nodejs.org/api/fs.html