Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between fs.exists and fs.existsSync

Tags:

node.js

fs

While working with the file I/O for node I found these two functions(fs.exists and fs.existsSync) to check if a file exists in the system. What are the differences between them?

like image 796
Mamun Reza Avatar asked Jan 07 '23 02:01

Mamun Reza


2 Answers

exists is non blocking, and you do subsequent work with the file through a callback.

existsSync is blocking and freezes your whole app while it is working. This can be appealing to new node users because they can continue their code on the next line. However, once you become used to using callbacks, this is a far inferior way to do things.

like image 65
trex005 Avatar answered Jan 22 '23 15:01

trex005


One is working in a synchronize way (wait until finished) and another return immediately and return a promise which has a future value.

like image 21
omer727 Avatar answered Jan 22 '23 13:01

omer727