What's the difference between check existing file synchronously and asynchronously in node.js?
For example:
var path = require('path');
if (path.existsSync("/the/path")) { // or fs.existsSync
// ...
}
and
// Is it a directory?
lstat('/the/path', function(err, stats) {
if (!err && stats.isDirectory()) {
// Yes it is
}
});
The synchronous versions of the fs
method provide their results via the method's return value; as a result these methods have to block while the I/O is performed to determine the result.
The asynchronous versions provide their results via the method's callback function that the caller provides as a parameter to the method. The methods just initiate the required I/O and then return immediately, so the return value from these methods isn't useful. When the I/O later completes, the callback is invoked to provide the result back to the caller.